public bool AddVertex(Vertex vertex) { try { vertexes.Add(vertex); vertexEdgeMapping.Add(vertex, new LinkedList<Edge>()); return true; } catch (Exception e) { Console.WriteLine("Add vertex failed! {0}", e.Message); return false; } }
public bool BreadthFirstSearch(Vertex rootVertex) { try { Console.WriteLine("******* Breadth First Search ********"); const string white = "white"; const string gray = "gray"; const string black = "black"; var colorMap = new Dictionary<Vertex, string>(); foreach (var vertex in vertexes) { colorMap.Add(vertex, white); } colorMap[rootVertex] = gray; var queue = new Queue<Vertex>(); queue.Enqueue(rootVertex); while (queue.Count != 0) { Vertex temp = queue.Dequeue(); foreach (Edge edge in vertexEdgeMapping[temp]) { if (colorMap[edge.ToVertex] == white) { colorMap[edge.ToVertex] = gray; queue.Enqueue(edge.ToVertex); } } colorMap[temp] = black; Console.WriteLine("Vertex {0} has been found!", temp.VertexLabel); } return true; } catch (Exception e) { Console.WriteLine("Breadth First Search failed! {0}", e.Message); return false; } }
public bool AddEdge(Vertex from, Vertex to, int weight) { try { var newEdge = new Edge(from, to, weight); vertexEdgeMapping[from].AddLast(newEdge); if (IsDirectGraph == true) { var backEdge = new Edge(to, from, weight); vertexEdgeMapping[to].AddLast(backEdge); } return true; } catch (Exception e) { Console.WriteLine("Add edge failed! {0}", e.Message); return false; } }
public void Graph_Tests() { // Direct Graph var G = new Graph(true); var u = new Vertex("u"); var v = new Vertex("v"); var w = new Vertex("w"); var x = new Vertex("x"); var y = new Vertex("y"); var z = new Vertex("z"); // Add vertexes G.AddVertex(u); G.AddVertex(v); G.AddVertex(w); G.AddVertex(x); G.AddVertex(y); G.AddVertex(z); // Add edges G.AddEdge(u, v, 3); G.AddEdge(u, x, 3); G.AddEdge(v, y, 3); G.AddEdge(w, y, 3); G.AddEdge(w, z, 3); G.AddEdge(x, v, 3); G.AddEdge(y, x, 3); G.AddEdge(z, u, 3); //Depth Search First G.DepthSearchFirst(); //Undirect Graph var UG = new Graph(false); var ur = new Vertex("r"); var us = new Vertex("s"); var ut = new Vertex("t"); var uu = new Vertex("u"); var uv = new Vertex("v"); var uw = new Vertex("w"); var ux = new Vertex("x"); var uy = new Vertex("y"); // Add vertexes UG.AddVertex(ur); UG.AddVertex(us); UG.AddVertex(ut); UG.AddVertex(uu); UG.AddVertex(uv); UG.AddVertex(uw); UG.AddVertex(ux); UG.AddVertex(uy); // Add Edge UG.AddEdge(ur, uv, 3); UG.AddEdge(ur, us, 3); UG.AddEdge(us, uw, 3); UG.AddEdge(ut, uu, 3); UG.AddEdge(ut, uw, 3); UG.AddEdge(ut, ux, 3); UG.AddEdge(uu, ux, 3); UG.AddEdge(uu, uy, 3); UG.AddEdge(uw, ux, 3); UG.AddEdge(ux, uy, 3); //Breadth First Search UG.BreadthFirstSearch(us); }
private bool DFS_Visit(Vertex vertex, Dictionary<Vertex, string> color) { try { const string white = "white"; const string gray = "gray"; const string black = "black"; color[vertex] = gray; foreach (Edge edge in vertexEdgeMapping[vertex]) { if (color[edge.ToVertex] == white) { DFS_Visit(edge.ToVertex, color); } } color[vertex] = black; Console.WriteLine("Vertex {0} has been found!", vertex.VertexLabel); return true; } catch (Exception e) { Console.WriteLine("DFS_Visit failed! {0}", e.Message); return false; } }