public void computeBFS(GraphAdjList adjLst, int V, int startVertex, Dictionary <int, string> map) { colors = new int[V]; parents = new int[V]; initializeArrays(V); Queue <int> q = new Queue <int>(); q.Enqueue(startVertex); while (q.Count != 0) { int element = q.Dequeue(); colors[element] = 1; if (adjLst.getAdj(element).Count != 0) { foreach (var vertex in adjLst.getAdj(element)) { if (colors[vertex.Item1] == -1) { colors[vertex.Item1] = 1; q.Enqueue(vertex.Item1); } } } Console.Write(map[element] + "\t"); } }
public void computeMST(GraphAdjList adjList, int vertices) { V = vertices; initializeArrays(); List <Tuple <int, int, int> > edges = new List <Tuple <int, int, int> >(); for (int i = 0; i < vertices; i++) { foreach (var v in adjList.getAdj(i)) { if (!(edges.Contains(new Tuple <int, int, int>(i, v.Item1, v.Item2)) || edges.Contains(new Tuple <int, int, int>(v.Item1, i, v.Item2)))) { edges.Add(new Tuple <int, int, int>(i, v.Item1, v.Item2)); } } } List <Tuple <int, int, int> > sortedEdges = new List <Tuple <int, int, int> >(edges.OrderBy(k => k.Item3)); foreach (var edge in sortedEdges) { if (!findCycle(edge)) { finalEdges.Add(edge); } } foreach (var v in finalEdges.OrderBy(k => k.Item1.ToString() + k.Item2.ToString())) { Console.WriteLine(v.Item1 + "\t" + v.Item2 + "\t" + v.Item3 + "\n"); } }
public void computeMST(GraphAdjList adjList, int start) { initialize(adjList); Q.updatePriority(new HeapClass() { data = start, priority = 0 }); while (Q.count() != 0) { HeapClass priorityElement = Q.getMax(); foreach (var v in adjList.getAdj(priorityElement.data)) { if (Q.contains(v.Item1) && -1 * v.Item2 > Q.getPriority(v.Item1)) { Q.updatePriority(new HeapClass() { data = v.Item1, priority = -1 * v.Item2 }); parents[v.Item1] = priorityElement.data; } } } Console.WriteLine("\nvertex\tparent"); for (int i = 0; i < V; i++) { Console.WriteLine("\n" + i + "\t\t" + parents[i]); } }
void DFSUtil(GraphAdjList adjList, Dictionary <int, string> map, int vertex, bool isStronglyConnectedModule) { time += 1; discover[vertex] = time; color[vertex] = 1; if (isStronglyConnectedModule) { Console.Write(vertex.ToString()); } foreach (var v in adjList.getAdj(vertex)) { if (color[v.Item1] == -1) { parent[v.Item1] = vertex; DFSUtil(adjList, map, v.Item1, isStronglyConnectedModule); } } time += 1; finish[vertex] = time; topologicalSortedGraph.AddFirst(vertex); }
public void computeShortestPaths(GraphAdjList adjList, int source) { initializeSingleSource(source); extractEdges(adjList); for (int i = 0; i < V; i++) { Q.insertHeap(new HeapClass() { data = i, priority = distances[i] }); } while (Q.count() != 0) { HeapClass elem = Q.getMax(); foreach (var v in adjList.getAdj(elem.data)) { relax(elem.data, v.Item1, v.Item2); } } Console.WriteLine("\nvertex\tparent\tdistance"); for (int i = 0; i < V; i++) { Console.WriteLine(i + "\t" + parents[i] + "\t" + (-1 * distances[i]).ToString()); } }
void extractEdges(GraphAdjList adjList) { for (int i = 0; i < V; i++) { foreach (var v in adjList.getAdj(i)) { edges.Add(new Tuple <int, int, int>(i, v.Item1, v.Item2)); } } }
private void initialize(GraphAdjList adjList) { for (int i = 0; i < V; i++) { foreach (var v in adjList.getAdj(i)) { if (!(edges.Contains(new Tuple <int, int, int>(i, v.Item1, v.Item2)) || edges.Contains(new Tuple <int, int, int>(v.Item1, i, v.Item2)))) { edges.Add(new Tuple <int, int, int>(i, v.Item1, v.Item2)); } } } for (int i = 0; i < V; i++) { Q.insertHeap(new HeapClass() { data = i, priority = -1000 }); parents[i] = -1; } }