public static void SetUpUnDirectedGraph(KarthicGraph <int> graph) { GraphNode <int> node1 = new GraphNode <int>(0); GraphNode <int> node2 = new GraphNode <int>(1); GraphNode <int> node3 = new GraphNode <int>(2); GraphNode <int> node4 = new GraphNode <int>(3); GraphNode <int> node5 = new GraphNode <int>(4); GraphNode <int> node6 = new GraphNode <int>(5); GraphNode <int> node7 = new GraphNode <int>(6); //Add vertices graph.AddNode(node1); graph.AddNode(node2); graph.AddNode(node3); graph.AddNode(node4); graph.AddNode(node5); graph.AddNode(node6); graph.AddNode(node7); //Add edges graph.AddUndirectedEdge(node1, node2); graph.AddUndirectedEdge(node1, node3); graph.AddUndirectedEdge(node1, node4); graph.AddUndirectedEdge(node2, node5); graph.AddUndirectedEdge(node5, node7); graph.AddUndirectedEdge(node4, node6); //graph.AddDirectedEdge(new GraphNode<int>(4), new GraphNode<int>(6)); }
private void button6_Click(object sender, EventArgs e) { //Implemenataion. Deep copy a graph KarthicGraph <int> graph = new KarthicGraph <int>(); TreeHelper.SetUpUnDirectedGraph(graph); KarthicGraph <int> deepcopiedgraph = DeepCopyGraph(graph); }
private void button9_Click(object sender, EventArgs e) { KarthicGraph <int> graph = new KarthicGraph <int>(); TreeHelper.SetUpGraph(graph); StringBuilder output = new StringBuilder(); graph.BreadthFirstSearch(graph.Root, output); this.textBox9.Text = output.ToString(); }
private void button3_Click(object sender, EventArgs e) { KarthicGraph <int> graph = new KarthicGraph <int>(); TreeHelper.SetUpUnDirectedGraph(graph); GraphNode <int> startnode = graph.FindNodeByValue(Convert.ToInt16(this.textBox4.Text)); GraphNode <int> endnode = graph.FindNodeByValue(Convert.ToInt16(this.textBox1.Text)); bool result = graph.IsRouteExists(startnode, endnode); this.textBox2.Text = (result == true) ? "Route Exists" : "No route exists"; }
public KarthicGraph <int> DeepCopyGraph(KarthicGraph <int> graph) { Hashtable ht = new Hashtable(); GraphNode <int> root = graph.Root; KarthicGraph <int> copygraph = new KarthicGraph <int>(); Queue <GraphNode <int> > myqueue = new Queue <GraphNode <int> >(); myqueue.Enqueue(root); //hashtable will have original node as key and copied node as data GraphNode <int> copynode = new GraphNode <int>(root.Data); ht.Add(root, copynode); copygraph.Root = copynode; while (myqueue.Count != 0) { GraphNode <int> parent = myqueue.Dequeue(); foreach (GraphNode <int> child in parent.Neighbors) { //make sure you check for exists.. //if exists means already added/visited etc.. //this will catch the undirected structre a -> b and b ->a if (!ht.ContainsKey(child)) { GraphNode <int> childcopy = new GraphNode <int>(child.Data); ht.Add(child, childcopy); myqueue.Enqueue(child); } //get the copy of the parent and add the created childcopy to its neighbor ((GraphNode <int>)ht[parent]).Neighbors.Add((GraphNode <int>)ht[child]); //else //{ // //here it mean the child is alredy visited and added to ht by its parent // //unidirection ..we are adding only the copy // ((GraphNode<int>)ht[child]).Neighbors.Add((GraphNode<int>)ht[parent]); // //we don't add to queue to prevent loop //} } } return(copygraph); }
public bool BuildGraph(int startx, int starty, int size, char endgoal, char[,] maze, GraphNode <char> parent, KarthicGraph <char> graph) { //Base case //check for outside bounds if (startx < 0 || startx >= size || starty < 0 || starty >= size) { return(false); } //check for obstacle if (maze[startx, starty] == '#') { return(false); } //check if already visited..to prevent loop if (maze[startx, starty] == '+') { return(false); } //when the code comes means the path is free and goal not found so traverse in all direction GraphNode <char> child = new GraphNode <char>(maze[startx, starty]); child.XAxis = startx; child.YAxis = starty; // child.Marked = true; graph.AddNode(child); graph.AddDirectedEdge(parent, child); //parent.Neighbors.Add(child); //check for goal if (maze[startx, starty] == endgoal) { return(true); } //Mark the current point as visited maze[startx, starty] = '+'; //search in north of the point if (BuildGraph(startx - 1, starty, size, endgoal, maze, child, graph)) { return(true); } //search in the east of the point if (BuildGraph(startx, starty + 1, size, endgoal, maze, child, graph)) { return(true); } //search in the south of the point if (BuildGraph(startx + 1, starty, size, endgoal, maze, child, graph)) { return(true); } //search in the west of the point if (BuildGraph(startx, starty - 1, size, endgoal, maze, child, graph)) { return(true); } //goal not found ..unmark the point maze[startx, starty] = '.'; // child.Marked = false; return(false); }