I am trying to convert my EdgeList
into a Adjacency List
and then preorder traverse
through it. I am pretty sure the conversion to Adjacency List
is working correctly, but I am having troubles with preorder traversing
through it. I have tried to do it with DFS
, but it gives me the wrong result.
Here are my edges:
{index1=0, index2=2}
{index1=3, index2=4}
{index1=1, index2=4}
{index1=0, index2=5}
{index1=2, index2=6}
{index1=1, index2=5}
{index1=2, index2=7}
And this is what the linked list looks like.
0 - 2 - 5
1 - 4 - 5
2 - 6 - 7
3 - 4
Or
linked[2, 5]
linked[4, 5]
linked[6, 7]
linked[4]
Now I want to preorder traverse through the graph, to get the result 0 - 2 - 6 - 7 - 5 - 1 - 4 - 3
.
0
2 5
6 7 1
4
3
I have tried using DFS
in my AdjacencyListGraph
, but this is the result I get 0 2 6 7 5
with this code:
public class AdjacencyListGraph {
private int V; // No. of vertices
// Array of lists for Adjacency List Representation
private LinkedList<Integer> adj[];
// Constructor
AdjacencyListGraph(int v) {
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
//Function to add an edge into the graph
void addEdge(int v, int w) {
adj[v].add(w); // Add w to v's list.
}
// A function used by DFS
void DFSUtil(int v,boolean visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v+" ");
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = adj[v].listIterator();
// System.out.println(i.toString());
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n,visited);
}
}
// The function to do DFS traversal. It uses recursive DFSUtil()
void DFS(int v)
{
// Mark all the vertices as not visited(set as
// false by default in java)
boolean visited[] = new boolean[V];
// Call the recursive helper function to print DFS traversal
// starting from all vertices one by one
DFSUtil(v, visited);
}
public static void main(String[] args) {
AdjacencyListGraph graph = new AdjacencyListGraph(8);
graph.addEdge(0, 2);
graph.addEdge(0, 5);
graph.addEdge(1, 4);
graph.addEdge(1, 5);
graph.addEdge(2, 6);
graph.addEdge(2, 7);
graph.addEdge(3, 4);
graph.DFS(0);
}
}
I have also tried using:
void DFS()
{
// Mark all the vertices as not visited(set as
// false by default in java)
boolean visited[] = new boolean[V];
// Call the recursive helper function to print DFS traversal
// starting from all vertices one by one
for (int i=0; i<V; ++i)
if (visited[i] == false)
DFSUtil(i, visited);
}
Instead of the one mentioned above and it goes through all of the nodes
, but it returns the result in wrong order.
What am I doing wrong and how or what should I use to get the desired result?
Try with this graph:
graph.addEdge(0, 2);
graph.addEdge(0, 5);
graph.addEdge(1, 4);
graph.addEdge(5, 1); // different direction than original
graph.addEdge(2, 6);
graph.addEdge(2, 7);
graph.addEdge(4, 3); // different direction than original
Or, make a slight modification in addEdge
method in your program:
//Function to add an edge into the graph
void addEdge(int v, int w) {
adj[v].add(w); // Add w to v's list.
adj[w].add(v); // Add v to w's list.
}