Question

Set up graph based on the file input below in .txt 7 Ari Bob Chio Dmitri...

Set up graph based on the file input below in .txt

7
Ari
Bob
Chio
Dmitri
Ehab
Gerald
Fritz
Ari Bob
Ari Chio
Bob Ari
Chio Ari
Chio Ehab
Chio Gerald
Dmitri Ehab
Dmitri Gerald
Ehab Chio
Ehab Dmitri
Gerald Chio
Gerald Dmitri
Gerald Fritz
Fritz Gerald

(Should be in display console)

Write a java program to Implementing Graph with Adjacency List:

Expected Result:

Ari-->Bob-->Chio

Bob-->Ari

Chio-->Ari-->Ehab-->Gerald

Dmitri-->Ehab-->Gerald

Ehab-->Chio-->Dmitri

Gerald-->Chio-->Dmitri-->Fritz

Fritz-->Gerald

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Firstly Know the meaning of
Adjacency List:

-> Adjacency List is the Array[] of Linked List, where array
size is same as number of Vertices in the graph.
Every Vertex has a Linked List.
Each Node in this Linked list represents the reference to the other
vertices which share an edge with the current vertex.
The weights can also be stored in the Linked List Node.

// now code

import java.util.LinkedList;

public class Graph {
int vertex;
LinkedList<Integer> list[];

public Graph(int vertex) {
this.vertex = vertex;
list = new LinkedList[vertex];
for (int i = 0; i <vertex ; i++) {
list[i] = new LinkedList<>();
}
}

public void addEdge(int source, int destination){

//add edge
list[source].addFirst(destination);

//add back edge ((for undirected)
list[destination].addFirst(source);
}

public void printGraph(){
for (int i = 0; i <vertex ; i++) {
if(list[i].size()>0) {
System.out.print("Vertex " + i + " is connected to: ");
for (int j = 0; j < list[i].size(); j++) {
System.out.print(list[i].get(j) + " ");
}
System.out.println();
}
}
}

public static void main(String[] args) {
Graph graph = new Graph(5);
graph.addEdge(0,1);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.printGraph();
}
}

Add a comment
Know the answer?
Add Answer to:
Set up graph based on the file input below in .txt 7 Ari Bob Chio Dmitri...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

  • Write a Java program called Histogram.java that displays a list of distinct characters in an input...

    Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance,...

  • [INPUT] First line number of vertex v adjacency matrix of graph Second v+1 line (If not connected, 1 If connected, weig...

    [INPUT] First line number of vertex v adjacency matrix of graph Second v+1 line (If not connected, 1 If connected, weight on the edge) [OUTPUT First line For completed MST Second line Completed MST cost as a result of running dfs(0) [EXAMPLE] input,txt 7 -1 28 -1 -1 -1 10 -1 28 -1 16 -1 -1 -1 14 -1 16 -1 12 -1 -1 -1 -1 -1 12 -1 22 -1 18 -1 -1 -1 22 -1 25 24 10...

  • Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS...

    Write a C++ program called ts.cpp that implements the topological sorting algorithm based on the DFS algorithm. Your program should read an input file name and determine if the input graph is a DAG (= directed acyclic graph) or not. If the graph is not a DAG, your program has to stop without further processing. However, if it’s a DAG, your program should display the starting node(s), popping-off order, and topologically sorted list. In the problem, you can assume that...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • Overview These exercises will allow you to have some practice with basic Java file Input/Output. In...

    Overview These exercises will allow you to have some practice with basic Java file Input/Output. In addition, you will also have an opportunity to have more practice with methods and arrays.   Objectives Practice with programming fundamentals Variables - Declaration and Assignment Primitive types Arithmetic Expressions Simple keyboard input and text display output Branching - if-elseif-else syntax Loops - simple while loops, nested while loops Methods - functions and procedures ArrayLists - collections of variables File I/O Works towards the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT