public UndirectedGraphBreadthFirst(UndirectedGraph g) { marked = new bool[g.getTotalVertices()]; distanceMatrix = new int[g.getTotalVertices()]; pathMatrix = new int[g.getTotalVertices()]; visitedMatrix = new State[g.getTotalVertices()]; }
private int[] size; //number of vertices in a connected component public ConnectedComponentGraph(UndirectedGraph g) { marked = new bool[g.getTotalVertices()]; id = new int[g.getTotalVertices()]; size = new int[g.getTotalVertices()]; for (int i = 0; i < g.getTotalVertices(); i++) { if (!marked[i]) { dfs(g, i); count++; } } }
public int[] getShortestPath(UndirectedGraph 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 void bfs(UndirectedGraph g, int vertex) { Queue<int> q = new Queue<int>(); //set up the entire visitedMatrix with -1 except for the vertex node for (int i = 0; i < g.getTotalVertices(); i++) { visitedMatrix[i] = State.Unvisited; } //mark root with 1 to sat it's visited visitedMatrix[vertex] = State.Visiting; q.Enqueue(vertex); while (q.Count > 0) { int v = q.Dequeue(); Console.WriteLine(v); marked[v] = true; //get all adjacent ArrayList adj = g.Adjacent(v); for (int i = 0; i < adj.Count; i++) { int currentVertex = (int) adj[i]; if (visitedMatrix[currentVertex] == State.Unvisited) { q.Enqueue(currentVertex); visitedMatrix[currentVertex] = State.Visiting; } } visitedMatrix[v] = State.Visited; marked[v] = false; } }
public Boolean isConnected(UndirectedGraph g) { return totalPaths == g.getTotalVertices(); }
public UndirectedGraphDepthFirst(UndirectedGraph graph) { marked = new bool[graph.getTotalVertices()]; path = new Queue<int>(); totalPaths = 0; }
public static void main() { UndirectedGraph g = new UndirectedGraph(13); g.AddEdge(0, 5); g.AddEdge(4, 5); g.AddEdge(0, 1); g.AddEdge(9, 12); g.AddEdge(6, 4); g.AddEdge(5, 4); g.AddEdge(0, 2); g.AddEdge(11, 12); g.AddEdge(9, 10); g.AddEdge(0, 6); g.AddEdge(7, 8); g.AddEdge(9, 11); g.AddEdge(5, 3); ConnectedComponentGraph cc = new ConnectedComponentGraph(g); Console.WriteLine("Total connected components: " + cc.numberOfConnectedComponent()); //display vertices in each connnected component Dictionary<int, Queue<int>> dict = new Dictionary<int, Queue<int>>(); for (int i = 0; i < cc.numberOfConnectedComponent(); i++) { dict[i] = new Queue<int>(); } for (int i = 0; i < g.getTotalVertices(); i++) { dict[cc.connectedComponentId(i)].Enqueue(i); } for (int i = 0; i < cc.numberOfConnectedComponent(); i++) { Console.WriteLine("\n"); Console.WriteLine("Component: " + i); while (dict[i].Count > 0) { Console.Write(dict[i].Dequeue() + " "); } } }