public int[] getPath(DirectedGraph g, int vertex) { Queue<int> q = new Queue<int>(); q.Enqueue(vertex); //set up the entire distance matrix with -1 except for the vertex node for (int i = 0; i < g.getTotalVertices(); i++) { distanceMatrix[i] = -1; } //set up current vertex with 0 because there will be no distance distanceMatrix[vertex] = 0; while (q.Count > 0) { int v = q.Dequeue(); Console.Write(v); //this is the breadth first traversal ArrayList adjacent = g.Adjacent(v); //loop through all the adjacent nodes, if the currentVertex is still -1, then go in and do process for (int i = 0; i < adjacent.Count; i++) { int currentVertex = (int)adjacent[i]; if (distanceMatrix[currentVertex] == -1) { //add one to the distanceMatrix here because it's going to be from v to the current path distanceMatrix[currentVertex] = distanceMatrix[v] + 1; pathMatrix[currentVertex] = v; q.Enqueue(currentVertex); } } } return distanceMatrix; }
public DirectedGraphDepthFirst(DirectedGraph g, int vertex) { marked = new bool[g.getTotalVertices()]; dfs(g, vertex); }
private int[] pathMatrix; //previous edge in shortest path public DirectedGraphBreadthFirst(DirectedGraph g) { marked = new bool[g.getTotalVertices()]; distanceMatrix = new int[g.getTotalVertices()]; pathMatrix = new int[g.getTotalVertices()]; }