Question

In: Computer Science

how do I create a graph class to store a adjacency list representation of a graph?...

how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.

Solutions

Expert Solution

Since you have not mentioned the language of your preference, I am providing the code in JAVA.

CODE

import java.util.LinkedList;

public class AdjacencyList

{

static class Graph

{

int V;

LinkedList<Integer> adjListNodes[];

// constructor

Graph(int V)

{

this.V = V;

// define the size of array as

// number of vertices

adjListNodes = new LinkedList[V];

// Create a new list for each vertex

// such that adjacent nodes can be stored

for(int i = 0; i < V ; i++){

adjListNodes[i] = new LinkedList<>();

}

}

}

public static void addEdge(Graph graph, int src, int dest)

{

graph.adjListNodes[src].add(dest);

// Since this is an undirected graph, need to add an edge from dest to src too

graph.adjListNodes[dest].add(src);

}

public static void displayGraph(Graph graph)

{

for(int v = 0; v < graph.V; v++)

{

System.out.println("Adjacency list of vertex "+ v);

System.out.print("head");

for(Integer pCrawl: graph.adjListNodes[v]){

System.out.print(" -> "+pCrawl);

}

System.out.println("\n");

}

}

public static void main(String args[])

{

// create the graph given in above figure

int V = 5;

Graph graph = new Graph(V);

addEdge(graph, 0, 1);

addEdge(graph, 0, 2);

addEdge(graph, 1, 3);

addEdge(graph, 1, 2);

addEdge(graph, 1, 0);

addEdge(graph, 2, 3);

addEdge(graph, 3, 4);

displayGraph(graph);

}

}


Related Solutions

Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list...
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list in term of searching for edge. 2. Topological sort runs in O(|V| + |E|) where |V| is the number of vertices, and |E| is the number of edges in the input graph. 3. If vertex u can reach vertex v, and vertex v can reach vertex u, then vertices u and v are in the same Strongly-connected component (SCC). 4. The Bellman-Ford algorithm will...
One simple representation for a graph is to use an adjacency matrix: each of the N...
One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Java Implementation It uses adjacency list representation, and already has the loadAdjList() function implemented for reading...
Java Implementation It uses adjacency list representation, and already has the loadAdjList() function implemented for reading adjacency lists from standard input (make sure you understand the input format and how loadAdjList() works). You need to complete the function printAdjMatrix(). import java.util.LinkedList; import java.util.Scanner; import java.util.Iterator; class Graph { private int totalVertex; private LinkedList<LinkedList<Integer>> adjList; //adjacency list of edges public Graph() { totalVertex = 0; } public void loadAdjList() { Scanner in = new Scanner(System.in); totalVertex = in.nextInt(); adjList = new...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
Create a Square Class and Create a graphical representation of your Square class - your class...
Create a Square Class and Create a graphical representation of your Square class - your class will have the following data fields: double width, String color. - provide a no-args constructor. - provide a constructor that creates a square with the specific width - implement method getArea() - implement method getPerimeter() - implement method setColor(). - draw a UML diagram for your class - write a test program that will create a square with the width 30, 40 and 50....
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
C++ program that converts a directed graph data from a user into a corresponding adjacency list...
C++ program that converts a directed graph data from a user into a corresponding adjacency list format. First it will read an input graph data from a user first, after it will convert it to the adjacency matrix format. Assume that the maximum number of vertices in the input graph is less than or equal to 50. The Sample Input 1 3 6 0 1 1 0 1 2 2 1 2 0 0 2 Output 1 0->1->2 1->0->2 2->0->1...
You are asked to create to to-do list program to store and manage to-do items for...
You are asked to create to to-do list program to store and manage to-do items for user. You need to define a class to-do item which holds the following attributes: • Id => number, generated by the program when item is created • Title => string • Description => string • Type (e.g. shopping, housing, work, etc.) => Enum • Priority => number between 1 to 5 • Status => (e.g. done, in progress, hold, etc.) => Enum • Create...
Use your calculator to complete the table. **How do I graph y=ex ***How do I graph...
Use your calculator to complete the table. **How do I graph y=ex ***How do I graph the Inverse of y=ex x y=ex -3 -2 -1 0 1 1 2 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT