static OrientedGraph OrientedGraphGenerator(int noVertices, int noEdges) { HashSet <Edge> edges = new HashSet <Edge>(); var rand = new Random(); OrientedGraph g = new OrientedGraph(); for (int i = 0; i < noVertices; i++) { g.AddVertex(); } for (int i = 0; i < noEdges; i++) { long weight = (long)rand.Next(1000000); int a = rand.Next(100000); int b = rand.Next(100000); Edge e = new Edge(a, b, weight); if (!edges.Contains(e)) { g.AddEdge(a, b, weight); edges.Add(e); } else { i--; } } return(g); }
/// <summary> /// This is example program that reads CSV file containing dependencies /// for installing different programs and outputs order in which /// programs have to be installed so that no dependencies are missing /// when programs are installed. /// </summary> public static void Main() { OrientedGraph og = new OrientedGraph(); // Read input data string[] lines = System.IO.File.ReadAllLines("dependencies.csv"); // Assign index to every program (create pairing - name, index) Dictionary <string, long> programs = new Dictionary <string, long>(); string [] names = new string[lines.Length]; int i = 0; // Parse input, add vertices to graph and remember their indices foreach (string line in lines) { string programName = line.Split(',')[0]; programs.Add(programName, i); names[i] = programName; og.AddVertex(i); i++; } // Add edges to a graph foreach (string line in lines) { string[] dependencies = line.Split(','); for (int j = 1; j < dependencies.Length; j++) { long destination = programs[dependencies[j]]; long source = programs[dependencies[0]]; og.AddEdge(source, destination); } } // Find topological ordering of dependencies List <long> topoOrder = Toposort.TopologicalOrdering(ref og); // Print order in which programs have to be installed Console.WriteLine("Install in following order:"); for (int j = 0; j < topoOrder.Count; j++) { Console.Write(names[topoOrder[j]] + " "); if (j + 1 < topoOrder.Count) { Console.Write("--> "); } } Console.WriteLine(); }
OrientedGraph oriented() { OrientedGraph og = new OrientedGraph(); for (int i = 0; i < 7; i++) { og.AddVertex(); } og.AddEdge(1, 2, 2); og.AddEdge(1, 3, 1); og.AddEdge(2, 3, 1); og.AddEdge(1, 4, 4); og.AddEdge(3, 6, 3); og.AddEdge(4, 5, 3); og.AddEdge(5, 6, 1); return(og); }
public void testSCCS() { OrientedGraph s = new OrientedGraph(); OrientedGraph og = oriented(); for (int i = 1; i < 9; i++) { s.AddVertex(); } s.AddEdge(1, 2); s.AddEdge(2, 3); s.AddEdge(3, 1); s.AddEdge(2, 4); s.AddEdge(2, 5); s.AddEdge(5, 6); s.AddEdge(6, 7); s.AddEdge(7, 5); int [] correct = new int[] { 4, 3, 3, 3, 2, 1, 1, 1 }; int [] res = Scc.FindSCCS(s); Assert.AreEqual(correct, res); Assert.AreEqual(new int[] { 7, 6, 5, 4, 3, 2, 1 }, Scc.FindSCCS(og)); }
public void testAddRemoveEdgesVertices() { // Not oriented graph Graph g = new Graph(); for (int i = 0; i < 6; i++) { g.AddVertex(); } g.AddEdge(2, 3); g.AddEdge(1, 3); g.AddEdge(2, 5); g.AddEdge(3, 5); Assert.False(g.RemoveEdge(1, 2)); Assert.False(g.RemoveEdge(5, 1)); Assert.True(g.RemoveEdge(1, 3)); Assert.True(g.RemoveEdge(2, 5)); Assert.False(g.RemoveEdge(2, 5)); // Oriented graph OrientedGraph og = new OrientedGraph(); for (int i = 0; i < 7; i++) { og.AddVertex(); } og.AddEdge(1, 4, 2); og.AddEdge(6, 1, 3); og.AddEdge(1, 3, 3); og.AddEdge(4, 6, 1); og.AddEdge(6, 4, 2); Assert.True(og.RemoveEdge(1, 4)); Assert.False(og.RemoveEdge(1, 4)); Assert.False(og.RemoveEdge(1, 6)); Assert.False(og.RemoveEdge(3, 1)); Assert.True(og.RemoveEdge(4, 6)); Assert.True(og.RemoveEdge(1, 3)); }