public static Graph PrimMST(Graph g) { if (g.Directed) { Output.WriteLine("Can't run Prim's algorithm on a directed graph"); return null; } Graph copy = new Graph(); Graph mst = new Graph(); //Create a copy to preserve g's state g.CopyTo(copy); List<Vertex> vertices = copy.GetVertices(); foreach (Vertex vert in vertices) { mst.AddVertex(vert); } PrimData v = new PrimData(); v.Predecessor = null; v.Seen = true; v.V = vertices[0]; v.D = 0; v.InMST = true; v.EdgeLength = float.PositiveInfinity; vertices[0].Tag = v; foreach (Vertex u in vertices) { if (!v.V.Equals(u)) { PrimData pd = new PrimData(); pd.D = float.PositiveInfinity; pd.V = u; pd.InMST = false; pd.Seen = false; u.Tag = pd; } } Heap<PrimData> q = new Heap<PrimData>(true); q.Add(vertices[0].Tag as PrimData); while (q.HasNext()) { PrimData pd = q.Next(); if (pd.Predecessor == null) { //mst.AddVertex(pd.V); } else { string label = pd.V.Label; mst.AddEdge(new Edge(pd.Predecessor, pd.V, pd.EdgeLength)); pd.V.Label = label; pd.InMST = true; } foreach (Edge e in pd.V.GetOutEdges()) { Vertex connectedVertex = e.GetToVertex(); PrimData data = connectedVertex.Tag as PrimData; if (!data.Seen) { q.Add(data); data.Predecessor = pd.V; data.EdgeLength = e.Weight; data.Seen = true; } if (data.D > e.Weight && !data.InMST) { data.D = e.Weight; data.Predecessor = pd.V; data.EdgeLength = e.Weight; q.Update(data); } } /*foreach (Edge e in pd.V.GetInEdges()) { Vertex connectedVertex = e.GetFromVertex(); PrimData data = connectedVertex.Tag as PrimData; if (!data.Seen) { q.Add(data); data.Predecessor = pd.V; data.EdgeLength = e.Weight; data.Seen = true; } if (data.D > e.Weight && !data.InMST) { data.D = e.Weight; data.Predecessor = pd.V; data.EdgeLength = e.Weight; q.Update(data); } }*/ } return mst; }
private void copyLinkedGraph(Graph to) { to.Clear(); to.Directed = this.Directed; for (int i = 0; i < this.GetVertices().Count; i++) { vertices[i].Tag = i; Vertex v = new Vertex(); v.Tag = i; to.AddVertex(v); } for (int i = this.GetEdges().Count-1; i >= 0; i--) { Edge currentEdge = this.GetEdges()[i]; Vertex v1 = new Vertex(); v1.Label = currentEdge.GetFromVertex().Label; int cd = (int)currentEdge.GetFromVertex().Tag; if ((int)to.GetVertices()[cd].Tag == cd) { v1 = to.GetVertices()[cd]; } Vertex v2 = new Vertex(); v2.Label = currentEdge.GetToVertex().Label; int cd2 = (int)currentEdge.GetToVertex().Tag; if ((int)to.GetVertices()[cd2].Tag == cd2) { v2 = to.GetVertices()[cd2]; } Edge e = currentEdge; e = new Edge(v1, v2, currentEdge.Weight); to.AddEdge(e); } }
public static Graph GenerateRandomGraph(int n) { float prob = 0.2f; Graph g = new Graph(); g.Directed = false; for (int i = 0; i < n; i++) { Vertex v = new Vertex(); g.AddVertex(v); } Random r = new Random(); //Randomly decide when to add an edge between any two pairs foreach (Vertex v in g.GetVertices()) { foreach (Vertex u in g.GetVertices()) { if (!v.Equals(u)) { double random = r.NextDouble(); if (random >= 0 && random <= prob) { g.AddEdge(new Edge(v, u, (float)r.NextDouble())); } } } } foreach (Vertex v in g.GetVertices()) { VertexClassification vc = ClassifyVertex(v); Output.WriteLine("Vertex " + v.ToString() + " is " + vc.ToString()); } return g; }
public static Graph KruskalMST(Graph g) { Graph copy = new Graph(); Graph mst = new Graph(); //Create a copy to preserve g's state g.CopyTo(copy); List<Edge> edges = copy.GetEdges(); List<Vertex> vertices = copy.GetVertices(); for (int i = 0; i < vertices.Count; i++) { //Associate each vertex with a set of vertices //Initially, each vertex belongs to its own set List<Vertex> vertexSet = new List<Vertex>(); vertexSet.Add(vertices[i]); vertices[i].Tag = vertexSet; mst.AddVertex(vertices[i]); } //We don't want to consider edges going in the opposite direction List<Edge> filteredEdges = new List<Edge>(); for (int i = 0; i < edges.Count; i += 2) { filteredEdges.Add(edges[i]); } filteredEdges.Sort(delegate(Edge e1, Edge e2) { return e1.Weight.CompareTo(e2.Weight); }); //Have counter that counts number of edges used. //If it equals the total number of edges then the original graph was disconnected int edgeIndex = 0; //Loop through all the edges while (edgeIndex < filteredEdges.Count/* && mst.GetEdges().Count < g.GetVertices().Count - 1*/) { //Walk up the array instead of removing the vertex Edge minEdge = filteredEdges[edgeIndex]; //Get the sets associated with the vertices on the minEdge List<Vertex> fromSet = minEdge.GetFromVertex().Tag as List<Vertex>; List<Vertex> toSet = minEdge.GetToVertex().Tag as List<Vertex>; //If these two sets are not the same set... if (!fromSet.Equals(toSet)) { //...then add the edge to the MST string fromLabel = minEdge.GetFromVertex().Label; string toLabel = minEdge.GetToVertex().Label; mst.AddEdge(minEdge); minEdge.GetFromVertex().Label = fromLabel; minEdge.GetToVertex().Label = toLabel; //Merge the two vertices' sets together List<Vertex> newList = fromSet.Union(toSet).ToList<Vertex>(); foreach (Vertex v in newList) { v.Tag = newList; } minEdge.GetFromVertex().Tag = newList; minEdge.GetToVertex().Tag = newList; } //Offset by 2 because we're ignoring edges that go in the opposite direction edgeIndex++; } return mst; }
private static void TestBFS() { Graph g = new Graph(); Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); Vertex v4 = new Vertex(); Vertex v5 = new Vertex(); Vertex v6 = new Vertex(); Vertex v7 = new Vertex(); g.AddVertex(v1); g.AddVertex(v2); g.AddVertex(v3); g.AddVertex(v4); g.AddVertex(v5); g.AddVertex(v6); g.AddVertex(v7); g.AddEdge(new Edge(v1, v2)); g.AddEdge(new Edge(v1, v3)); g.AddEdge(new Edge(v2, v6)); g.AddEdge(new Edge(v2, v4)); g.AddEdge(new Edge(v6, v4)); g.AddEdge(new Edge(v4, v5)); List<Edge> path = GraphAlgorithms.BreadthFirstSearch(g, 0, 4); }
public static Graph CreateResidualNetwork(FlowNetwork fn) { FlowNetwork copy = new FlowNetwork(); fn.CopyTo(copy); Graph g = new Graph(); g.Directed = true; for (int i = 0; i < copy.GetVertices().Count; i++) { Vertex v = copy.GetVertices()[i]; v.Tag = i; v.RemoveAllEdges(); g.AddVertex(v); } foreach (FlowEdge e in copy.GetEdges()) { float backflow = e.CurrentFlow; float capacity = e.Capacity; float forwardFlow = capacity - backflow; Vertex fromVertex = e.GetFromVertex(); Vertex toVertex = e.GetToVertex(); int fromIndex = (int)fromVertex.Tag; int toindex = (int)toVertex.Tag; Vertex newTo = g.GetVertices()[toindex]; Vertex newFrom = g.GetVertices()[fromIndex]; if (backflow > 0) { Edge backEdge = new Edge(newTo, newFrom, backflow); g.AddEdge(backEdge); } if (forwardFlow > 0) { Edge forwardEdge = new Edge(newFrom, newTo, forwardFlow); g.AddEdge(forwardEdge); } } return g; }
private static void TestAllPairs() { //Graph for figure 4.15 part B Graph g = new Graph(); Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); Vertex v4 = new Vertex(); Vertex v5 = new Vertex(); Vertex v6 = new Vertex(); Vertex v7 = new Vertex(); Vertex v8 = new Vertex(); Vertex v9 = new Vertex(); Vertex v10 = new Vertex(); Vertex v11 = new Vertex(); Vertex v12 = new Vertex(); g.AddVertex(v1); g.AddVertex(v2); g.AddVertex(v3); g.AddVertex(v4); g.AddVertex(v5); g.AddVertex(v6); g.AddVertex(v7); g.AddVertex(v8); g.AddVertex(v9); g.AddVertex(v10); g.AddVertex(v11); g.AddVertex(v12); g.AddEdge(new Edge(v1, v2, 5)); g.AddEdge(new Edge(v1, v4, 10)); g.AddEdge(new Edge(v2, v3, 7)); g.AddEdge(new Edge(v2, v5, 1)); g.AddEdge(new Edge(v3, v6, 4)); g.AddEdge(new Edge(v4, v5, 3)); g.AddEdge(new Edge(v5, v6, 3)); g.AddEdge(new Edge(v1, v2, 5)); g.AddEdge(new Edge(v4, v7, 11)); g.AddEdge(new Edge(v1, v2, 5)); g.AddEdge(new Edge(v5, v8, 7)); g.AddEdge(new Edge(v6, v9, 5)); g.AddEdge(new Edge(v7, v10, 9)); g.AddEdge(new Edge(v7, v8, 2)); g.AddEdge(new Edge(v8, v11, 1)); g.AddEdge(new Edge(v8, v9, 0)); g.AddEdge(new Edge(v9, v12, 12)); g.AddEdge(new Edge(v11, v12, 4)); float[,] d = GraphAlgorithms.AllPairsShortestPath(g); GraphAlgorithms.PrintMatrix(d, g.GetVertices().Count); }
private static void TestMatrixStuff() { Graph g = new Graph(); g.Directed = true; Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); Vertex v4 = new Vertex(); g.AddVertex(v1); g.AddVertex(v2); g.AddVertex(v3); g.AddVertex(v4); g.AddEdge(new Edge(v2, v1, 25f)); g.AddEdge(new Edge(v1, v3, 10f)); g.RemoveEdge(g.GetEdges()[1]); g.AddEdge(new Edge(v4, v2, 7f)); g.RemoveVertex(v1); g.RemoveVertex(v2); g.AddEdge(new Edge(v3, v4, 30f)); g.AddEdge(new Edge(v4, v3, 27f)); Console.WriteLine("Testing graph insertion and deletion"); g.PrintMatrix(); Console.WriteLine(); Graph g2 = new Graph(); g.CopyTo(g2); g2.PrintMatrix(); }
private static void TestLabelCorrecting() { //Graph for figure 5.10 part A Graph g = new Graph(); g.Directed = true; Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); Vertex v4 = new Vertex(); Vertex v5 = new Vertex(); Vertex v6 = new Vertex(); g.AddEdge(new Edge(v1, v2, 10)); g.AddEdge(new Edge(v1, v3, 15)); g.AddEdge(new Edge(v2, v3, 25)); g.AddEdge(new Edge(v3, v2, -20)); g.AddEdge(new Edge(v2, v4, 0)); g.AddEdge(new Edge(v2, v5, 5)); g.AddEdge(new Edge(v4, v5, -5)); g.AddEdge(new Edge(v5, v4, 10)); g.AddEdge(new Edge(v5, v3, 30)); Console.WriteLine("Label Correcting Output:"); Graph labelCorrect = GraphAlgorithms.LabelCorrecting(g, 0); }
private static void TestDominatingSets() { Graph g = new Graph(); g.Directed = false; Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); Vertex v4 = new Vertex(); Vertex v5 = new Vertex(); Vertex v6 = new Vertex(); Vertex v7 = new Vertex(); g.AddEdge(new Edge(v1, v2)); g.AddEdge(new Edge(v2, v3)); g.AddEdge(new Edge(v3, v4)); g.AddEdge(new Edge(v4, v5)); g.AddEdge(new Edge(v5, v6)); g.AddEdge(new Edge(v6, v7)); //GraphAlgorithms.MinimumDominatingSet(g); g.AddEdge(new Edge(v7, v1)); GraphAlgorithms.MinimumDominatingSet(g); }
private static void TestDijkstra() { //Graph for figure 4.15 part A Graph g = new Graph(); Vertex u1 = new Vertex(); Vertex u2 = new Vertex(); Vertex u3 = new Vertex(); Vertex u4 = new Vertex(); Vertex u5 = new Vertex(); Vertex u6 = new Vertex(); g.AddEdge(new Edge(u1, u2, 2)); g.AddEdge(new Edge(u1, u3, 8)); g.AddEdge(new Edge(u2, u3, 5)); g.AddEdge(new Edge(u2, u4, 3)); g.AddEdge(new Edge(u3, u2, 6)); g.AddEdge(new Edge(u3, u5, 0)); g.AddEdge(new Edge(u4, u3, 1)); g.AddEdge(new Edge(u4, u5, 7)); g.AddEdge(new Edge(u4, u6, 6)); g.AddEdge(new Edge(u5, u4, 4)); g.AddEdge(new Edge(u6, u5, 2)); Console.WriteLine("Dijkstra Output:"); Graph tree = GraphAlgorithms.Dijkstra(g, 0, true); }