public static void Dijkstra <T>(RGGraph <T> graph, RGVertex <T> source) { List <RGVertex <T> > unfinishedVertices = new List <RGVertex <T> >(); foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Distance = int.MaxValue; vertex.Parent = null; unfinishedVertices.Add(vertex); } source.Distance = 0; while (unfinishedVertices.Count > 0) { RGVertex <T> vertex = GetClosestVertex(unfinishedVertices); unfinishedVertices.Remove(vertex); foreach (RGVertex <T> adjVertex in graph.GetAdjacentVertices(vertex)) { if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex)) { adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex); adjVertex.Parent = vertex; } } } }
public static void BreadthFirstSearch <T>(RGGraph <T> graph, RGVertex <T> sourceVertex) { foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Parent = null; vertex.Distance = 0; vertex.Visited = false; } Queue <RGVertex <T> > queue = new Queue <RGVertex <T> >(); queue.Enqueue(sourceVertex); while (queue.Count > 0) { RGVertex <T> vertex = queue.Dequeue(); foreach (RGVertex <T> neighbour in graph.GetAdjacentVertices(vertex)) { if (!neighbour.Visited) { neighbour.Parent = vertex; neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour); neighbour.Visited = true; queue.Enqueue(neighbour); } } vertex.Visited = true; } }
void TestDirectedGraph() { StringBuilder sb = new StringBuilder(); sb.Append("Begin Testing Directed Graph").AppendLine(); RGVertex <string> V1 = new RGVertex <string>("v1"); RGVertex <string> V2 = new RGVertex <string>("v2"); RGVertex <string> V3 = new RGVertex <string>("v3"); RGVertex <string> V4 = new RGVertex <string>("v4"); List <RGVertex <string> > Vertices = new List <RGVertex <string> > { V1, V2, V3, V4 }; RGGraph <string> graph = new RGGraph <string>(Vertices); graph.CreateDirectedEdge(V1, V2, 3); graph.CreateDirectedEdge(V4, V1, 1); graph.CreateDirectedEdge(V2, V3, 1); graph.CreateDirectedEdge(V2, V4, -5); sb.Append(graph.ToString()).AppendLine(); sb.Append("End of Testing Directed Graph").AppendLine(); debugClass.WriteStringToFile(DirectedGraphFile, sb.ToString()); }
public static List <RGVertex <T> > DepthFirstSearchWithGoal <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, RGVertex <T> goalVertex, bool reverseNeighbours = false) { if (sourceVertex.Equals(goalVertex)) { return(new List <RGVertex <T> > { sourceVertex }); } foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Parent = null; vertex.Distance = 0; vertex.Visited = false; } Stack <RGVertex <T> > stack = new Stack <RGVertex <T> >(); stack.Push(sourceVertex); while (stack.Count > 0) { RGVertex <T> vertex = stack.Pop(); List <RGVertex <T> > neighbours = graph.GetAdjacentVertices(vertex); if (reverseNeighbours) { neighbours.Reverse(); } foreach (RGVertex <T> neighbour in neighbours) { if (!neighbour.Visited) { neighbour.Parent = vertex; neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour); neighbour.Visited = true; if (neighbour.Equals(goalVertex)) { return(GetPathToSource <T>(neighbour)); } stack.Push(neighbour); } } vertex.Visited = true; } // no path found return(null); }
public static List <RGVertex <T> > BreadthFirstSearchWithGoal <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, RGVertex <T> goalVertex) { if (sourceVertex.Equals(goalVertex)) { return(new List <RGVertex <T> > { sourceVertex }); } foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Parent = null; vertex.Distance = 0; vertex.Visited = false; } Queue <RGVertex <T> > queue = new Queue <RGVertex <T> >(); queue.Enqueue(sourceVertex); while (queue.Count > 0) { RGVertex <T> vertex = queue.Dequeue(); foreach (RGVertex <T> neighbour in graph.GetAdjacentVertices(vertex)) { if (!neighbour.Visited) { neighbour.Parent = vertex; neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour); neighbour.Visited = true; if (neighbour.Equals(goalVertex)) { return(GetPathToSource <T>(neighbour)); } queue.Enqueue(neighbour); } } vertex.Visited = true; } // no path to goal vertex return(null); }
public static List <RGVertex <T> > DijkstraWithGoal <T>(RGGraph <T> graph, RGVertex <T> source, RGVertex <T> goal) { if (source.Equals(goal)) { return(new List <RGVertex <T> > { source }); } List <RGVertex <T> > unfinishedVertices = new List <RGVertex <T> >(); foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Distance = int.MaxValue; vertex.Parent = null; unfinishedVertices.Add(vertex); } source.Distance = 0; while (unfinishedVertices.Count > 0) { RGVertex <T> vertex = GetClosestVertex(unfinishedVertices); unfinishedVertices.Remove(vertex); if (vertex.Equals(goal)) { return(GetPathToSource(vertex)); } foreach (RGVertex <T> adjVertex in graph.GetAdjacentVertices(vertex)) { if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex)) { adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex); adjVertex.Parent = vertex; } } } return(null); }
// DFS BEGINS public static void DepthFirstSearch <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, bool reverseNeighbours = false) { foreach (RGVertex <T> vertex in graph.Vertices) { vertex.Parent = null; vertex.Distance = 0; vertex.Visited = false; } Stack <RGVertex <T> > stack = new Stack <RGVertex <T> >(); stack.Push(sourceVertex); while (stack.Count > 0) { RGVertex <T> vertex = stack.Pop(); List <RGVertex <T> > neighbours = graph.GetAdjacentVertices(vertex); if (reverseNeighbours) { neighbours.Reverse(); } foreach (RGVertex <T> neighbour in neighbours) { if (!neighbour.Visited) { neighbour.Parent = vertex; neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour); neighbour.Visited = true; stack.Push(neighbour); } } vertex.Visited = true; } }
void TestBFS() { StringBuilder sb = new StringBuilder(); sb.Append("Begin BFS test").AppendLine(); RGVertex <string> V1 = new RGVertex <string>("V1"); RGVertex <string> V2 = new RGVertex <string>("V2"); RGVertex <string> V3 = new RGVertex <string>("V3"); RGVertex <string> V4 = new RGVertex <string>("V4"); RGVertex <string> V5 = new RGVertex <string>("V5"); RGVertex <string> V6 = new RGVertex <string>("V6"); RGVertex <string> V7 = new RGVertex <string>("V7"); List <RGVertex <string> > vertices = new List <RGVertex <string> > { V1, V2, V3, V4, V5, V6, V7 }; RGGraph <string> graph = new RGGraph <string>(vertices); graph.CreateUnDirectedEdge(V4, V5); graph.CreateUnDirectedEdge(V4, V2); graph.CreateUnDirectedEdge(V4, V1); graph.CreateUnDirectedEdge(V5, V6); graph.CreateUnDirectedEdge(V2, V5); graph.CreateUnDirectedEdge(V2, V7); graph.CreateUnDirectedEdge(V2, V1); graph.CreateUnDirectedEdge(V1, V3); graph.CreateUnDirectedEdge(V1, V7); graph.CreateUnDirectedEdge(V7, V6); string graphData = graph.ToString(); sb.Append(graphData).AppendLine(); sb.Append("Running BFS from : ").Append(V4.ToString()).AppendLine(); RGSearchAlgorithms.BreadthFirstSearch <string>(graph, V4); List <RGVertex <string> > fromV6 = RGSearchAlgorithms.GetPathToSource <string>(V6); sb.Append("Start vertex : V6").AppendLine(); foreach (RGVertex <string> vertex in fromV6) { sb.Append(vertex.ToString()); } sb.AppendLine(); List <RGVertex <string> > path = RGSearchAlgorithms.BreadthFirstSearchWithGoal <string>(graph, V6, V3); sb.Append("Path from v3 to v6").AppendLine(); foreach (RGVertex <string> vertex in path) { sb.Append(vertex.ToString()); } sb.AppendLine(); sb.Append("End BFS test").AppendLine(); debugClass.WriteStringToFile(BFSFile, sb.ToString()); }