Menu

(Solved) : Graph Class Import Javautillinkedlist Public Class Graph Int Numvertices Linkedlist Adjace Q37169316 . . .

Soukaina Fali Boubrahimi CSc 2720 Data Structures: Lab 1:1 Due Date: Sunday 21 Apr, 2019 11:59pm Given the adjacency list rep

Graph Class inport java.util.LinkedList; public class Graph t int nunvertices; LinkedListeInteger>ti adjacencylist; Graph(int

————————————- Graph Class——————————————

import java.util.LinkedList;

public class Graph {

int numVertices;

LinkedList[] adjacencyList;

Graph(int n){

numVertices = n;

adjacencyList = new LinkedList[numVertices];

for(int i=0;

i();

}

void addEdge(Integer src, Integer des)

{

this.adjacencyList[src].add(des);

}

void printGraph()

{

for(int i=0; i “);

}

if

(j==adjacencyList[i].size()-1)System.out.println(this.adjacencyList[i].get(j));

else

System.out.println();

}

}

public static Integer[][] generateAdjMatrix(){

Integer[][] adjacencyMatrix = null;

// INSERT CODE HERE

return adjacencyMatrix;

}

public static void printMatrix(Integer[][] adjMatrix){

// INSERT CODE HERE

}

public static void main(String[] args) {

// Create a graph of 5 vertices

Graph g2 = new Graph(5);

g2.addEdge(0, 1);

g2.addEdge(0, 4);

g2.addEdge(0, 3);

g2.addEdge(2, 0);

g2.addEdge(3, 2);

g2.addEdge(4, 3);

g2.addEdge(4, 1);

g2.printGraph();

Integer[][] adjMatrix = generateAdjMatrix();

printMatrix(adjMatrix);

/* Should print the following Matrix

0 1 0 1 1

0 0 0 0 0

1 0 0 0 0

0 0 1 0 0

0 1 0 1 0

*/

}

}

Soukaina Fali Boubrahimi CSc 2720 Data Structures: Lab 1:1 Due Date: Sunday 21 Apr, 2019 11:59pm Given the adjacency list representation af a graph, write a function generatehdjMatrix that generates and returns the adjaceney matrix representatian of the graph based on the adjacency list. Write another function printMatrix that takes the adjacency matrix input and print it For example: For the follawing graph: Input: Adjacency List: 2: e 3: 2 4:3-1 output: The adjacency Matrix: 811 1 Graph Class inport java.util.LinkedList; public class Graph t int nunvertices; LinkedListeInteger>ti adjacencylist; Graph(int n) nurVertices n for(int i-e;i4nunvertices;i++) adjacencylist[i] new LirkedisteIntegers void addEdge(Integer src, Integer desf this.adjacencyList[sre].add (des); void printGraph for(int 1-e; this.nunvertics+) Systen.out.printin(“Adjacancy 11st of vertex : “) Syste.out.print(..+1+.. : .); int j-8i for( j<this,adjacencylist[i].size()-1;j) Systen.out.print(this.adjacencylist[1-) Syster. out . print(” ..> .); if(i -adiacencyList[il.size()-1) Systen, out ,printIn(this.adjacencyList [ǐ].get(j) ); else Systen.out.p intin( public static Integer senerateldiMatrix) Integer'[][] adjacencyMatrix nulli return adjacencyMatrix; public static void printMatrix(Integer public static void nain(String] arRs) adjMatrix)i INSERT CODE HER Create a raph of 5 vertices Graph g2-neu Graph (5); 82.acdEdga(e, ); 82.acdEdg(e, 4): R2.addEdge(e, 3 C2,addLdge(2, 9) e2.acdEdge(a, 2); 82.acdEdga(4, 3); 82.acdEdge(4. 1): R2.printGraph) Intege Ii adjMatrix- generateAdjateixO printMatrix(adNatrix); Should print the following Matrix 011 1 a1014 Show transcribed image text Soukaina Fali Boubrahimi CSc 2720 Data Structures: Lab 1:1 Due Date: Sunday 21 Apr, 2019 11:59pm Given the adjacency list representation af a graph, write a function generatehdjMatrix that generates and returns the adjaceney matrix representatian of the graph based on the adjacency list. Write another function printMatrix that takes the adjacency matrix input and print it For example: For the follawing graph: Input: Adjacency List: 2: e 3: 2 4:3-1 output: The adjacency Matrix: 811 1
Graph Class inport java.util.LinkedList; public class Graph t int nunvertices; LinkedListeInteger>ti adjacencylist; Graph(int n) nurVertices n for(int i-e;i4nunvertices;i++) adjacencylist[i] new LirkedisteIntegers void addEdge(Integer src, Integer desf this.adjacencyList[sre].add (des); void printGraph for(int 1-e; this.nunvertics+) Systen.out.printin(“Adjacancy 11st of vertex : “) Syste.out.print(..+1+.. : .); int j-8i for( j .); if(i -adiacencyList[il.size()-1) Systen, out ,printIn(this.adjacencyList [ǐ].get(j) ); else Systen.out.p intin( public static Integer senerateldiMatrix) Integer'[][] adjacencyMatrix nulli return adjacencyMatrix; public static void printMatrix(Integer public static void nain(String] arRs) adjMatrix)i INSERT CODE HER Create a raph of 5 vertices Graph g2-neu Graph (5); 82.acdEdga(e, ); 82.acdEdg(e, 4): R2.addEdge(e, 3 C2,addLdge(2, 9) e2.acdEdge(a, 2); 82.acdEdga(4, 3); 82.acdEdge(4. 1): R2.printGraph) Intege Ii adjMatrix- generateAdjateixO printMatrix(adNatrix); Should print the following Matrix 011 1 a1014

Expert Answer


Answer to ————————————- Graph Class —————————————— import java.util.LinkedLis…

OR