public void AddStep(RenderTargetBitmap bitmap, GraphAdjList <string> graph) { Grid grid = new Grid(); ColumnDefinition newColumnDefinition = new ColumnDefinition(); //newColumnDefinition.Width = 1; grid.ColumnDefinitions.Add(newColumnDefinition); grid.ColumnDefinitions.Add(new ColumnDefinition()); Image image = new Image(); image.Width = bitmap.Width / 2; image.Height = bitmap.Height / 2; image.Source = bitmap; grid.Children.Add(image); Grid.SetColumn(image, 0); ControlLibrary_Graph.GraphAdjListShow graphAdjListShow = new ControlLibrary_Graph.GraphAdjListShow(); graphAdjListShow.Graph = graph; grid.Children.Add(graphAdjListShow); Grid.SetColumn(graphAdjListShow, 1); grids.AddLast(grid); this.topologicalSortImages.ItemsSource = grids; }
void BFS(GraphAdjList G, int s) { var queue = new Queue <int>(); queue.Enqueue(s); distTo[s] = 0; while (queue.Count != 0) { int v = queue.Dequeue(); foreach (var w in G.GetAdj(v)) { //if we reach the destination node, clear the queue to stop the search. if (w == e) { distTo[w] = distTo[v] + 1; edgeTo[w] = v; queue.Clear(); break; } if (distTo[w] == -1) { queue.Enqueue(w); distTo[w] = distTo[v] + 1; edgeTo[w] = v; } } } }
public void BreadthFirstTest() { var nodes = new GraphNode <int> [8]; for (int i = 0; i < nodes.Length; i++) { nodes[i] = new GraphNode <int>(i + 1); } var graph = new GraphAdjList <int>(nodes); graph.SetEdge(nodes[0], nodes[1], 1); graph.SetEdge(nodes[0], nodes[2], 1); graph.SetEdge(nodes[1], nodes[3], 1); graph.SetEdge(nodes[1], nodes[4], 1); graph.SetEdge(nodes[3], nodes[7], 1); graph.SetEdge(nodes[4], nodes[7], 1); graph.SetEdge(nodes[2], nodes[5], 1); graph.SetEdge(nodes[2], nodes[6], 1); graph.SetEdge(nodes[5], nodes[6], 1); foreach (var node in graph.BreadthFirst()) { Console.WriteLine(node.Data.Data); } }
private IGraph <string> ReadDirectedGraphFromString(string graphString) { var graph = new GraphAdjList <string>(); var lines = graphString.Split(Environment.NewLine).Where(s => !string.IsNullOrEmpty(s)).ToList(); foreach (var line in lines) { string[] strings = line.Split("-"); Vertex <string> v = new Vertex <string> { Value = strings[0] }; graph.Vertices.Add(v); } foreach (var line in lines) { string[] strings = line.Split("-"); Vertex <string> vertex = graph.Vertices.First(v => v.Value == strings[0]); foreach (string neighbour in strings[1].Split(",")) { if (neighbour != "") { var edge = new Edge <string>() { From = vertex, To = graph.Vertices.First(v => v.Value == neighbour) }; graph.AddEdges(new [] { edge }); } } } return(graph); }
private void ButtonClick_SubmitGraph(object sender, RoutedEventArgs e) { this.graph = new ClassLibrary_Graph.GraphAdjList <string>(this.graphShowControl.Graph); topoSort = new LinkedList <VexNode <string> >(); ShowGraphAdjListShowWindow(); ShowCriticalPathWindow(); ShowTopologicalSortWindow(); }
public void Create_Graph_With_6_Nodes_Add_Link_From_0To1_Assert_AreLinked_ZeroToOne() { var graph = new GraphAdjList(new int[] { 0, 1, 2, 3, 4, 5 }); Assert.AreEqual(6, graph.NumberOfVertices); graph.AddDirectedEdge(0, 1); var neighbours = graph.GetNeighbours(0); Assert.AreEqual(1, neighbours[0]); }
void Start() { nodes = new Node <string> [8]; graph = new GraphAdjList <string>(nodes); nodes[0] = new Node <string>("a"); graph.SetNode(0, nodes[0]); nodes[1] = new Node <string>("b"); graph.SetNode(1, nodes[1]); nodes[2] = new Node <string>("c"); graph.SetNode(2, nodes[2]); nodes[3] = new Node <string>("d"); graph.SetNode(3, nodes[3]); nodes[4] = new Node <string>("e"); graph.SetNode(4, nodes[4]); nodes[5] = new Node <string>("f"); graph.SetNode(5, nodes[5]); nodes[6] = new Node <string>("g"); graph.SetNode(6, nodes[6]); nodes[7] = new Node <string>("h"); graph.SetNode(7, nodes[7]); graph.SetEdge(graph.GetNode(0), graph.GetNode(1), 100); graph.SetEdge(graph.GetNode(0), graph.GetNode(2), 150); graph.SetEdge(graph.GetNode(1), graph.GetNode(3), 200); graph.SetEdge(graph.GetNode(1), graph.GetNode(4), 50); graph.SetEdge(graph.GetNode(3), graph.GetNode(7), 75); graph.SetEdge(graph.GetNode(4), graph.GetNode(7), 300); graph.SetEdge(graph.GetNode(2), graph.GetNode(5), 300); graph.SetEdge(graph.GetNode(2), graph.GetNode(6), 300); graph.SetEdge(graph.GetNode(5), graph.GetNode(6), 300); //for (int i = 0; i < graph.GetNumOfVertex(); i++) //{ // Debug.Log(graph.GetNode(i).Data); //} //for (int i = 0; i < graph.GetNumOfVertex(); i++) //{ // for (int j = 0; j < graph.GetNumOfVertex(); j++) // { // Debug.Log(graph.GetEdge(i, j)); // } //} //graph.BFSTravel(); //graph.DFSTravel(); }
public void Create_Graph_With_6_Nodes_Add_Another_Node_Add_Link_From_5To6_Assert_AreLinked_ZeroToOne() { var graph = new GraphAdjList(new int[] { 0, 1, 2, 3, 4, 5 }); Assert.AreEqual(6, graph.NumberOfVertices); graph.AddVertex(); graph.AddDirectedEdge(5, 6); var neighbours = graph.GetNeighbours(5); Assert.AreEqual(6, neighbours[0]); }
public void DFS(GraphAdjList G, int v) { marked[s] = true; foreach (var w in G.GetAdj(v)) { if (!marked[w]) { DFS(G, w); edgeTo[w] = v; } } }
public BreadthFirstSearch(GraphAdjList G, int s) { edgeTo = new int[G.VertexCount]; distTo = new int[G.VertexCount]; for (int i = 0; i < G.VertexCount; i++) { distTo[i] = -1; edgeTo[i] = -1; } this.s = s; BFS(G, s); }
public void TestAdjacencyListGraphTraversal() { GraphAdjList g = new GraphAdjList(4); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(2, 3); g.AddEdge(3, 3); string dfs = g.DepthFirstTraversal(2).Trim(); Assert.IsTrue(string.Equals(dfs, "2 0 1 3", System.StringComparison.OrdinalIgnoreCase)); string bfs = g.BreadthFirstTraversal(2).Trim(); Assert.IsTrue(string.Equals(bfs, "2 0 3 1", System.StringComparison.OrdinalIgnoreCase)); }
public int RunSimulation() { if (Data.UniqueListOfNodes.Count == null || Data.UniqueListOfNodes.Count < 2) { Console.WriteLine("\n\nTrivial or non-existent graph, exiting."); return(0); } //Get the user to enter the starting and destination node int startNode = getUserToEnterNode(1); int destinationNode = getUserToEnterNode(2); if (startNode == destinationNode) { Console.WriteLine("\n\nTrivial case, i.e. distance from node to itself is 0, exiting."); return(0); } Console.WriteLine("\n\nRunning simulation."); //Generate and populate the GraphAdjList object GraphAdjList graph = new GraphAdjList(Data.UniqueListOfNodes.Count()); for (int i = 0; i < Data.IndexI.Count; i++) { graph.AddEdge(Data.IndexI[i], Data.IndexJ[i]); } BreadthFirstSearch bfs = new BreadthFirstSearch(graph, startNode, destinationNode); if (bfs.distTo[destinationNode] != -1) { Console.WriteLine("\n\nNode {0} and Node {1} linked after {2} steps. Exiting.", startNode, destinationNode, bfs.distTo[destinationNode]); } else { Console.WriteLine("\n\nNode {0} and Node {1} are not linked. Exiting.", startNode, destinationNode); } return(1); }
void BFS(GraphAdjList G, int s) { var queue = new Queue <int>(); queue.Enqueue(s); distTo[s] = 0; while (queue.Count != 0) { int v = queue.Dequeue(); foreach (var w in G.GetAdj(v)) { if (distTo[w] == -1) { queue.Enqueue(w); distTo[w] = distTo[v] + 1; edgeTo[w] = v; } } } }
public void Create_Graph_With_7_Nodes_One_At_A_Time() { var graph = new GraphAdjList(new int[] { 0 }); graph.AddVertex(); graph.AddVertex(); graph.AddVertex(); graph.AddDirectedEdge(1, 2); graph.AddDirectedEdge(2, 0); graph.AddVertex(); graph.AddDirectedEdge(0, 3); graph.AddVertex(); graph.AddDirectedEdge(3, 4); graph.AddVertex(); graph.AddDirectedEdge(5, 4); graph.AddVertex(); graph.AddDirectedEdge(6, 5); graph.AddDirectedEdge(6, 4); var zerosNeighbours = graph.GetNeighbours(0); var onesNeighbours = graph.GetNeighbours(1); var twosNeighbours = graph.GetNeighbours(2); var threessNeighbours = graph.GetNeighbours(3); var foursNeighbours = graph.GetNeighbours(4); var fivesNeighbours = graph.GetNeighbours(5); var sixsNeighbours = graph.GetNeighbours(6); Assert.AreEqual(3, zerosNeighbours[0]); Assert.IsTrue(sixsNeighbours.Contains(5)); Assert.IsTrue(sixsNeighbours.Contains(4)); Assert.IsTrue(onesNeighbours.Contains(2)); Assert.IsTrue(twosNeighbours.Contains(0)); Assert.IsTrue(threessNeighbours.Contains(4)); Assert.IsTrue(foursNeighbours.Count == 0); Assert.IsTrue(fivesNeighbours.Contains(4)); }
public void Set(GraphAdjList <string> graph, Image image, int[] e, int[] l, int[] sub, bool[] flag, Edge <string>[] edges, int[] ve, int[] vl, int[] topo_sort_index) { this.CriticalPathGrid.Children.Add(image); Grid.SetColumn(image, 1); Grid.SetRow(image, 1); this.e = e; this.l = l; this.sub = sub; this.flag = flag; this.edges = edges; this.ve = ve; this.vl = vl; this.topo_sort_index = topo_sort_index; for (int i = 0; i < graph.GetNumOfVertex(); i++) { VexCPInfo newVexInfo = new VexCPInfo(); newVexInfo.Data = graph.GetVexNode(i).Data.Data; newVexInfo.Ve = ve[i].ToString(); newVexInfo.Vl = vl[i].ToString(); vexCPData.Add(newVexInfo); } for (int i = 0; i < graph.GetNumOfEdge(); i++) { EdgeCPInfo newEdgeInfo = new EdgeCPInfo(); newEdgeInfo.Edge = edges[i].Tail + "-" + edges[i].Head; newEdgeInfo.E = e[i].ToString(); newEdgeInfo.L = l[i].ToString(); newEdgeInfo.Sub = sub[i].ToString(); EdgeCPData.Add(newEdgeInfo); } //VexDataGrid.DataContext = vexCPData; //EdgeDataGrid.DataContext = EdgeCPData; VexDataGrid.ItemsSource = vexCPData; EdgeDataGrid.ItemsSource = EdgeCPData; }
public DepthFirstSearch(GraphAdjList G, int s) { marked = new bool[G.VertexCount]; edgeTo = new int[G.VertexCount]; this.s = s; }
public void Create_Graph_With_6_Nodes_Assert_NumberOfVertex_6() { var graph = new GraphAdjList(new int[] { 0, 1, 2, 3, 4, 5 }); Assert.AreEqual(6, graph.NumberOfVertices); }
public FFFF() { GraphAdjList <int> xx = new GraphAdjList <int>(); IGraph <int> xxx = xx; }
public void MergePlanes() { GraphAdjList planeGraph = new GraphAdjList(extractionPlaneRec.Count); for (int i = 0; i < extractionPlaneRec.Count; i++) { for (int j = i + 1; j < extractionPlaneRec.Count; j++) { double dis1 = 0; for (int loop = 0; loop < extractionPlaneRec[i].Value; loop++) { dis1 += Math.Abs(extractionPlaneRec[i].Points[loop].x * extractionPlaneRec[j].NormalABC.x + extractionPlaneRec[i].Points[loop].y * extractionPlaneRec[j].NormalABC.y + extractionPlaneRec[i].Points[loop].z * extractionPlaneRec[j].NormalABC.z - extractionPlaneRec[j].D) / extractionPlaneRec[j].D; } dis1 /= extractionPlaneRec[i].Value; double dis2 = 0; for (int loop = 0; loop < extractionPlaneRec[j].Value; loop++) { dis2 += Math.Abs(extractionPlaneRec[j].Points[loop].x * extractionPlaneRec[i].NormalABC.x + extractionPlaneRec[j].Points[loop].y * extractionPlaneRec[i].NormalABC.y + extractionPlaneRec[j].Points[loop].z * extractionPlaneRec[i].NormalABC.z - extractionPlaneRec[i].D) / extractionPlaneRec[i].D; } dis2 /= extractionPlaneRec[j].Value; double dis = (dis1 * extractionPlaneRec[j].Value + dis2 * extractionPlaneRec[i].Value) / (extractionPlaneRec[i].Value + extractionPlaneRec[j].Value); if (extractionPlaneRec[i].NormalABC.Dot(extractionPlaneRec[j].NormalABC) > Math.Cos(10 * Math.PI / 180) && dis < 0.04) { planeGraph.AddEdge(i, j); } } } int groupIdx = 0; int[] clusterInd = new int[extractionPlaneRec.Count]; bool[] visited = new bool[extractionPlaneRec.Count]; DepthFirstSearch dfs = new DepthFirstSearch(); for (int i = 0; i < extractionPlaneRec.Count; i++) { if (!visited[i]) { dfs.DFS(planeGraph, ref visited, i, ref clusterInd, groupIdx); groupIdx++; } } mergedPlaneRec = new List <pointPlaneClass>(); for (int i = 0; i < groupIdx; i++) { mergedPlaneRec.Add(new pointPlaneClass(i, 0)); } for (int i = 0; i < extractionPlaneRec.Count; i++) { mergedPlaneRec[clusterInd[i]].Value += extractionPlaneRec[i].Value; mergedPlaneRec[clusterInd[i]].Points.AddRange(extractionPlaneRec[i].Points); mergedPlaneRec[clusterInd[i]].PointsIdx.AddRange(extractionPlaneRec[i].PointsIdx); foreach (int pi in extractionPlaneRec[i].PointsIdx) { pointLabels[pi] = clusterInd[i]; } } for (int i = 0; i < mergedPlaneRec.Count; i++) { MyVector4 returnedvalue = pointToPlaneClustering.RANSACNormal(mergedPlaneRec[i].Points, null, 0.0002, 0.9, 100); mergedPlaneRec[i].NormalABC = new MyVector3(returnedvalue.x, returnedvalue.y, returnedvalue.z); mergedPlaneRec[i].D = returnedvalue.w; } }
private IGraph <MazeField> BuildGraphFromMaze(TwoDMaze maze) { GraphAdjList <MazeField> graph = new GraphAdjList <MazeField>(); List <Vertex <MazeField> > vertices = new List <Vertex <MazeField> >(); int W = maze.Fields.GetLength(0); int H = maze.Fields.GetLength(1); int[] ii = new[] { 0, 0, 1, -1 }; int[] jj = new[] { 1, -1, 0, 0 }; var verticeGrid = new Vertex <MazeField> [W, H]; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { if (maze.Fields[i, j].FieldType == FieldType.Obstacle) { continue; } verticeGrid[i, j] = new Vertex <MazeField>() { Value = maze.Fields[i, j] }; } } for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { if (maze.Fields[i, j].FieldType == FieldType.Obstacle) { continue; } for (int step = 0; step < 4; step++) { int nextX = i + ii[step]; int nextY = j + jj[step]; if (nextY >= H || nextY < 0) { continue; } if (nextX >= W || nextX < 0) { continue; } if (maze.Fields[nextX, nextY].FieldType == FieldType.Clear) { Edge <MazeField> edgeTo = new Edge <MazeField>() { From = verticeGrid[i, j], To = verticeGrid[nextX, nextY] }; Edge <MazeField> edgeFrom = new Edge <MazeField>() { To = verticeGrid[i, j], From = verticeGrid[nextX, nextY] }; graph.AddEdges(new [] { edgeFrom, edgeTo }); } } vertices.Add(verticeGrid[i, j]); } } graph.AddVertices(vertices); return(graph); }