Question: Please complete Depth first search adjacency list function. note: I have included the constructor and image of adjacency list and text file, i have also
Please complete Depth first search adjacency list function.
note: I have included the constructor and image of adjacency list and text file, i have also included display function


// default constructor
public GraphLists(String graphFile) throws IOException
{
int u, v;
int e, wgt;
FileReader fr = new FileReader(graphFile);
BufferedReader reader = new BufferedReader(fr);
String splits = " +"; // multiple whitespace as delimiter
String line = reader.readLine();
String[] parts = line.split(splits);
System.out.println("Parts[] = " + parts[0] + " " + parts[1]);
V = Integer.parseInt(parts[0]);
E = Integer.parseInt(parts[1]);
// create sentinel node
z = new Node();
z.next = z;
// create adjacency lists, initialised to sentinel node z
visited = new int[V+1];
adj = new Node[V+1];
for(v = 1; v
adj[v] = z;
// read the edges
System.out.println("Reading edges from text file");
for(e = 1; e
{
line = reader.readLine();
parts = line.split(splits);
// u and v are two vertices
u = Integer.parseInt(parts[0]);
v = Integer.parseInt(parts[1]);
wgt = Integer.parseInt(parts[2]);
System.out.println("Edge " + toChar(u) + "--(" + wgt + ")--" + toChar(v));
Node t = new Node();
t.vert = v;
t.wgt = wgt;
t.next = adj[u];
adj[u] = t;
Node x = new Node();
x.vert = u;
x.wgt = wgt;
x.next = adj[v];
adj[v] = x;
}
}
public void display() {
int v;
Node n;
for(v=1; v
System.out.print(" adj[" + toChar(v) + "] ->" );
for(n = adj[v]; n != z; n = n.next)
System.out.print(" |" + toChar(n.vert) + " | " + n.wgt + "| ->");
}
System.out.println("");
}
public void depth_first_iterative(int s) {
int u,v;
Node t;
;
for(v = 1; v
visited[v] = 0;
}
//TODO
}
wgraph3.txt - No... File Edit Format View Help 7 11 1 2 7 1 4 5 23 8 | 2 4 9 25 7 35 5 4 5 15 4 6 5 6 8 5 7 9 67 11 111 On reading in all 11 edges: 0 1 45 27 2 57 419 38 17 3 3 55 28 4 66 515 29 15 5 79 68 4 | 15 35217 6 7 | 11 58 46 7 6 11 59
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
