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 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.
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
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement: 1)Object get(int), which takes an int index and returns the Object at that index 2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference Both your...
How do I make the series graph
How do I make the series graph
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT