public SymbolGraph(String filename, Char delimiter) { //create all as vertices symbolTable = new Dictionary<string, int>(); allLines = System.IO.File.ReadAllLines(@filename); int count = 0; foreach (string line in allLines) { String[] items = line.Split(delimiter); for(int i = 0; i < items.Length; i++){ if(!symbolTable.ContainsKey(items[i])){ symbolTable[items[i]] = count; count++; } } } //invert index to get string keys as an array keys = new String[symbolTable.Count]; count = 0; foreach (KeyValuePair<string, int> pair in symbolTable) { keys[count] = pair.Key; count++; } //create graph from dictionary graph = new UndirectedGraph(symbolTable.Count); for (int i = 0; i < allLines.Length; i++) { String[] items = allLines[i].Split(delimiter); for (int j = 1; j < items.Length; j++) { graph.AddEdge(symbolTable[items[0]], symbolTable[items[j]]); } } }
public static void main() { UndirectedGraph g = new UndirectedGraph(13); g.AddEdge(0, 6); g.AddEdge(0, 2); g.AddEdge(0, 1); g.AddEdge(0, 5); g.AddEdge(1, 0); g.AddEdge(2, 0); g.AddEdge(3, 5); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(4, 6); g.AddEdge(4, 3); g.AddEdge(5, 3); g.AddEdge(5, 4); g.AddEdge(5, 0); g.AddEdge(6, 0); g.AddEdge(6, 4); g.AddEdge(7, 8); g.AddEdge(8, 7); g.AddEdge(9, 11); g.AddEdge(9, 10); g.AddEdge(9, 12); g.AddEdge(10, 9); g.AddEdge(11, 9); g.AddEdge(11, 12); g.AddEdge(12, 11); g.AddEdge(12, 9); }
public static void main() { UndirectedGraph g = new UndirectedGraph(13); g.AddEdge(0, 6); g.AddEdge(0, 2); g.AddEdge(0, 1); g.AddEdge(0, 5); g.AddEdge(1, 0); g.AddEdge(2, 0); g.AddEdge(3, 5); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(4, 6); g.AddEdge(4, 3); g.AddEdge(5, 3); g.AddEdge(5, 4); g.AddEdge(5, 0); g.AddEdge(6, 0); g.AddEdge(6, 4); g.AddEdge(7, 8); g.AddEdge(8, 7); g.AddEdge(9, 11); g.AddEdge(9, 10); g.AddEdge(9, 12); g.AddEdge(10, 9); g.AddEdge(11, 9); g.AddEdge(11, 12); g.AddEdge(12, 11); g.AddEdge(12, 9); UndirectedGraphDepthFirst depthFirst = new UndirectedGraphDepthFirst(g); depthFirst.search(g, 8); Queue<int> q = depthFirst.getPath(); Console.WriteLine("Path is: "); foreach (int value in q) { Console.Write(value + " "); } for(int i = 0; i < g.getTotalEdges(); i++) { if (depthFirst.pathExists(i) != false) { Console.Write(i); } } Console.WriteLine("Connected: " + depthFirst.isConnected(g)); }
public static void main() { UndirectedGraph g = new UndirectedGraph(13); g.AddEdge(0, 5); g.AddEdge(4, 5); g.AddEdge(0, 1); g.AddEdge(9, 12); g.AddEdge(6, 4); g.AddEdge(5, 4); g.AddEdge(0, 2); g.AddEdge(11, 12); g.AddEdge(9, 10); g.AddEdge(0, 6); g.AddEdge(7, 8); g.AddEdge(9, 11); g.AddEdge(5, 3); ConnectedComponentGraph cc = new ConnectedComponentGraph(g); Console.WriteLine("Total connected components: " + cc.numberOfConnectedComponent()); //display vertices in each connnected component Dictionary<int, Queue<int>> dict = new Dictionary<int, Queue<int>>(); for (int i = 0; i < cc.numberOfConnectedComponent(); i++) { dict[i] = new Queue<int>(); } for (int i = 0; i < g.getTotalVertices(); i++) { dict[cc.connectedComponentId(i)].Enqueue(i); } for (int i = 0; i < cc.numberOfConnectedComponent(); i++) { Console.WriteLine("\n"); Console.WriteLine("Component: " + i); while (dict[i].Count > 0) { Console.Write(dict[i].Dequeue() + " "); } } }
static void Main(string[] args) { UndirectedGraph g = new UndirectedGraph(6); g.AddEdge(0, 2); g.AddEdge(0, 1); g.AddEdge(0, 5); g.AddEdge(1, 0); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(2, 1); g.AddEdge(2, 3); g.AddEdge(2, 4); g.AddEdge(3, 5); g.AddEdge(3, 4); g.AddEdge(3, 2); g.AddEdge(4, 3); g.AddEdge(4, 2); g.AddEdge(5, 3); g.AddEdge(5, 0); UndirectedGraphBreadthFirst br = new UndirectedGraphBreadthFirst(g); int[] shortestPath = br.getShortestPath(g,5); //br.bfs(g, 5); }
public static void main() { UndirectedGraph g = new UndirectedGraph(6); g.AddEdge(0, 2); g.AddEdge(0, 1); g.AddEdge(0, 5); g.AddEdge(1, 0); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(2, 1); g.AddEdge(2, 3); g.AddEdge(2, 4); g.AddEdge(3, 5); g.AddEdge(3, 4); g.AddEdge(3, 2); g.AddEdge(4, 3); g.AddEdge(4, 2); g.AddEdge(5, 3); g.AddEdge(5, 0); UndirectedGraphBreadthFirst br = new UndirectedGraphBreadthFirst(g); //int[] shortestPath = br.getShortestPath(g, 0); br.bfs(g, 5); }