public static void Main() { var graph = new Graph( new[] { new List<int> { 3, 6 }, // children of node 0 (Ruse) new List<int> { 2, 3, 4, 5, 6 }, // children of node 1 (Sofia) new List<int> { 1, 4, 5 }, // children of node 2 (Pleven) new List<int> { 0, 1, 5 }, // children of node 3 (Varna) new List<int> { 1, 2, 6 }, // children of node 4 (Bourgas) new List<int> { 1, 2, 3 }, // children of node 5 (Stara Zagora) new List<int> { 0, 1, 4 } // children of node 6 (Plovdiv) }, new string[] { "Ruse", "Sofia", "Pleven", "Varna", "Bourgas", "Stara Zagora", "Plovdiv" }); // Print the nodes and their children for (int nodeIndex = 0; nodeIndex < graph.ChildNodes.Length; nodeIndex++) { Console.WriteLine( "{0} -> {1}", nodeIndex, string.Join(" ", graph.ChildNodes[nodeIndex])); } Console.WriteLine(); // Print the node names and their children names for (int nodeIndex = 0; nodeIndex < graph.ChildNodes.Length; nodeIndex++) { Console.WriteLine( "{0} -> {1}", graph.NodeNames[nodeIndex], string.Join(", ", graph.ChildNodes[nodeIndex].Select(node => graph.NodeNames[node]))); } }
static void Main(string[] args) { Graph graph = new Graph(new int[][] { new int[] {3, 6}, // successors of vertice 0 new int[] {2, 3, 4, 5, 6}, // successors of vertice 1 new int[] {1, 4, 5}, // successors of vertice 2 new int[] {0, 1, 5}, // successors of vertice 3 new int[] {1, 2, 6}, // successors of vertice 4 new int[] {1, 2, 3}, // successors of vertice 5 new int[] {0, 1, 4} // successors of vertice 6 }); graph.DFSRecursive(0); }
static void Main(string[] args) { Graph g = new Graph(new List<int>[] { new List<int> {3, 6}, // successors of vertice 0 new List<int> {2, 3, 4, 5, 6}, // successors of vertice 1 new List<int> {1, 4, 5}, // successors of vertice 2 new List<int> {0, 1, 5}, // successors of vertice 3 new List<int> {1, 2, 6}, // successors of vertice 4 new List<int> {1, 2, 3}, // successors of vertice 5 new List<int> {0, 1, 4} // successors of vertice 6 }); for (int i = 0; i < g.childNodes.Length; i++) { Console.Write(i + " -> "); for (int j = 0; j < g.childNodes[i].Count; j++) { Console.Write(g.childNodes[i][j] + " "); } Console.WriteLine(); } }