public void test3() { UndirectedGraph<int, Edge<int>> graph = new UndirectedGraph<int, Edge<int>>(true); // Add vertices to the graph graph.AddVertex(0); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddVertex(4); // Create the edges Edge<int> e0_1 = new Edge<int>(0, 1); Edge<int> e0_2 = new Edge<int>(0, 2); Edge<int> e1_4 = new Edge<int>(1, 4); Edge<int> e3_4 = new Edge<int>(3, 4); Edge<int> e0_3 = new Edge<int>(0, 3); Edge<int> e1_2 = new Edge<int>(1, 2); // Add the edges graph.AddEdge(e0_1); graph.AddEdge(e0_2); graph.AddEdge(e1_2); graph.AddEdge(e1_4); graph.AddEdge(e3_4); graph.AddEdge(e0_3); List<int> path = new List<int>(); HamiltonianDefiner definer = new HamiltonianDefiner(graph); bool isHamiltonian = definer.isHamiltonianGraph(path); Assert.AreEqual(isHamiltonian, true); }
public void CreateEmptyGraphAndAddVertices() { var graph = new UndirectedGraph(); Assert.AreEqual(0, graph.Vertices()); Assert.AreEqual(0, graph.Edges()); int vertex1id = graph.AddVertex(); int vertex2id = graph.AddVertex(); Assert.AreEqual(0, vertex1id); Assert.AreEqual(1, vertex2id); graph.AddEdge(0, 1); Assert.AreEqual(2, graph.Vertices()); Assert.AreEqual(1, graph.Edges()); Assert.IsTrue(graph.AdjacentVertices(0).Contains(1)); Assert.IsTrue(graph.AdjacentVertices(1).Contains(0)); // add the same edge again graph.AddEdge(0, 1); Assert.AreEqual(2, graph.Vertices()); Assert.AreEqual(2, graph.Edges()); Assert.IsTrue(graph.AdjacentVertices(0).Contains(1)); Assert.IsTrue(graph.AdjacentVertices(1).Contains(0)); }
public void CloneTest() { var graph = new UndirectedGraph(); var newVertex1 = new Vertex("test-1"); var newVertex2 = new Vertex("test-2"); var newVertex3 = new Vertex("test-3"); graph.AddVertex(newVertex1); graph.AddVertex(newVertex2); graph.AddVertex(newVertex3); var newEdge1 = new UndirectedEdge(newVertex1, newVertex2); var newEdge2 = new UndirectedEdge(newVertex1, newVertex3); graph.AddEdge(newEdge1); graph.AddEdge(newEdge2); var clonedGraph = graph.Clone() as IGraph; Assert.IsTrue(clonedGraph is UndirectedGraph); Assert.AreEqual(graph.VerticesCount, clonedGraph.VerticesCount); foreach (var vertex in graph.Vertices) { var clonedVertex = clonedGraph.Vertices.Single(v => v.Equals(vertex)); Assert.AreNotSame(clonedVertex, vertex); } Assert.AreEqual(graph.EdgesCount, clonedGraph.EdgesCount); foreach (var clonedEdge in clonedGraph.Edges) { Assert.IsTrue(clonedEdge is UndirectedEdge); var edge = graph.Edges.Single(e => e.Equals(clonedEdge)); Assert.AreNotSame(edge, clonedEdge); } }
public void UndirectedDijkstraSimpleGraph() { var undirectedGraph = new UndirectedGraph <object, Edge <object> >(true); object v1 = "vertex1"; object v2 = "vertex2"; object v3 = "vertex3"; var e1 = new Edge <object>(v1, v2); var e2 = new Edge <object>(v2, v3); var e3 = new Edge <object>(v3, v1); undirectedGraph.AddVertex(v1); undirectedGraph.AddVertex(v2); undirectedGraph.AddVertex(v3); undirectedGraph.AddEdge(e1); undirectedGraph.AddEdge(e2); undirectedGraph.AddEdge(e3); var algorithm = new UndirectedDijkstraShortestPathAlgorithm <object, Edge <object> >( undirectedGraph, edge => 1.0); var observer = new UndirectedVertexPredecessorRecorderObserver <object, Edge <object> >(); using (observer.Attach(algorithm)) algorithm.Compute(v1); Assert.IsTrue(observer.TryGetPath(v3, out _)); }
private List <UndirectedGraph <int, IEdge <int> > > FindComponents(UndirectedGraph <int, IEdge <int> > g) { List <UndirectedGraph <int, IEdge <int> > > components = new List <UndirectedGraph <int, IEdge <int> > >(); HashSet <int> vertices = g.Vertices.ToHashSet(); while (vertices.Count > 0) { UndirectedGraph <int, IEdge <int> > component = new UndirectedGraph <int, IEdge <int> >(false); Queue <int> queue = new Queue <int>(); int v = vertices.First(); vertices.Remove(v); queue.Enqueue(v); component.AddVertex(v); while (queue.Count > 0) { v = queue.Dequeue(); foreach (int n in g.AdjacentVertices(v)) { if (vertices.Contains(n)) { component.AddVertex(n); queue.Enqueue(n); vertices.Remove(n); } component.AddEdge(new Edge <int>(v, n)); } } components.Add(component); } return(components); }
private void Initialize() { _graph = new UndirectedGraph <PointVertex, Edge <PointVertex> >(); _nodes.Clear(); foreach (Obstacle obstacle in _obstacles) { foreach (RotationTreeNode node in obstacle.Nodes) { _nodes.Add(node); _graph.AddVertex(new PointVertex(node.Point)); } _graph.AddEdgeRange(obstacle.Segments.Select(s => new Edge <PointVertex>(new PointVertex(s.Point1.Point), new PointVertex(s.Point2.Point)))); } foreach (Point point in _singlePoints) { Obstacle obstacle = _obstacles.FirstOrDefault(o => o.Contains(point)); var newPoint = new RotationTreeNode(obstacle, point, true); _graph.AddVertex(new PointVertex(point)); _nodes.Add(newPoint); } double maxX = _nodes.Max(p => p.Point.X); _plusInf = new RotationTreeNode(new Point(maxX + 100, double.PositiveInfinity)); _minusInf = new RotationTreeNode(new Point(maxX + 100, double.NegativeInfinity)); _plusInf.AddChild(_minusInf); foreach (RotationTreeNode node in _nodes.OrderByDescending(n => n)) { _minusInf.AddChild(node); } }
public void IsEulerianManyComponents() { // Eulerian UndirectedGraph <int, UndirectedEdge <int> > graph = CreateUndirectedGraph(new[] { new Vertices(1, 2), new Vertices(2, 3), new Vertices(1, 3) }); graph.AddVertex(4); graph.AddVertex(5); AssertIsEulerian(true, graph); // Not Eulerian graph = CreateUndirectedGraph(new[] { new Vertices(1, 2), new Vertices(2, 3), new Vertices(1, 3), new Vertices(4, 5), new Vertices(5, 6), new Vertices(4, 6) }); graph.AddVertex(7); AssertIsEulerian(false, graph); }
private List <NodeConfig> DetermineMavens() { var mavens = new List <NodeConfig>(); var graph = new UndirectedGraph <string, Edge <string> >(); foreach (var config in _configs) { graph.AddVertex(config.CurrentNode.Name); foreach (var connection in config.Connections) { graph.AddVertex(connection.Name); graph.AddEdge(new Edge <string>(config.CurrentNode.Name, connection.Name)); } } var components = new Dictionary <string, int>(); graph.ConnectedComponents(components); foreach (var i in components.Values.Distinct()) { var names = components.Where(p => p.Value == i).Select(p => p.Key); var node = _configs.Where(config => names.Contains(config.CurrentNode.Name)) .OrderByDescending(x => x.Connections.Count).First(); mavens.Add(node); } return(mavens); }
public void IsomorphismTest() { var graph1 = new UndirectedGraph(); var vertexA = new Vertex("A"); var vertexB = new Vertex("B"); var vertexC = new Vertex("C"); var vertexD = new Vertex("D"); var graph2 = new UndirectedGraph(); var vertex1 = new Vertex("1"); var vertex2 = new Vertex("2"); var vertex3 = new Vertex("3"); var vertex4 = new Vertex("4"); graph1.AddEdge(new UndirectedEdge(vertexB, vertexC)); graph2.AddEdge(new UndirectedEdge(vertex1, vertex3)); graph1.AddEdge(new UndirectedEdge(vertexC, vertexD)); graph2.AddEdge(new UndirectedEdge(vertex3, vertex4)); graph2.AddVertex(vertex1); graph2.AddVertex(vertex2); graph2.AddVertex(vertex3); graph2.AddVertex(vertex4); graph1.AddVertex(vertexA); graph1.AddVertex(vertexB); graph1.AddVertex(vertexC); graph1.AddVertex(vertexD); Assert.IsTrue(GraphOperations.CheckIsomorphism(graph1, graph2)); }
public void TestGraphWithOneVertex() { Graph graph = new UndirectedGraph(); try { graph.IsConnected(); Assert.Fail(); } catch (InvalidOperationException) { //test passed } Vertex v1 = new("v1"); graph.AddVertex(v1); Assert.IsTrue(graph.IsConnected()); Vertex v2 = new("v2"); graph.AddVertex(v2); Assert.IsFalse(graph.IsConnected()); graph.AddEdge(new UndirectedEdge(new Vertex[] { v1, v2 }, "e1")); Assert.IsTrue(graph.IsConnected()); Assert.ThrowsException <InvalidGraphStructureException>(() => graph.AddEdge(new UndirectedEdge(new Vertex[] { v2, v1 }, "e2"))); Assert.IsTrue(graph.IsConnected()); }
public void Repro42450() { var ug = new UndirectedGraph <object, Edge <object> >(true); object v1 = "vertex1"; object v2 = "vertex2"; object v3 = "vertex3"; var e1 = new Edge <object>(v1, v2); var e2 = new Edge <object>(v2, v3); var e3 = new Edge <object>(v3, v1); ug.AddVertex(v1); ug.AddVertex(v2); ug.AddVertex(v3); ug.AddEdge(e1); ug.AddEdge(e2); ug.AddEdge(e3); var udspa = new UndirectedDijkstraShortestPathAlgorithm <object, QuickGraph.Edge <object> >(ug, edge => (double)1); var observer = new UndirectedVertexPredecessorRecorderObserver <object, Edge <object> >(); using (observer.Attach(udspa)) udspa.Compute(v1); IEnumerable <QuickGraph.Edge <object> > path; observer.TryGetPath(v3, out path); }
private void Connect(AcadGeo.Point3d fp, AcadGeo.Point3d sp) { Node fn = FindNode(fp); if (null == fn) { fn = new Node(fp); routers.AddVertex(fn); } Node sn = FindNode(sp); if (null == sn) { sn = new Node(sp); routers.AddVertex(sn); } if (routers.ContainsEdge(fn, sn)) { return; } Connection conn = new Connection(fn, sn); routers.AddEdge(conn); fn.Connections.Add(conn); sn.Connections.Add(conn); }
public void Return_100_Given_Graph_With_4_Vertexes_And_5_Edges() { // Arrange var graph = new UndirectedGraph(); var a = new Vertex("A"); var b = new Vertex("B"); var c = new Vertex("C"); var d = new Vertex("D"); graph.AddVertex(a); graph.AddVertex(b); graph.AddVertex(c); graph.AddVertex(d); graph.AddEdge(a, b, 10); graph.AddEdge(b, c, 20); graph.AddEdge(a, d, 30); graph.AddEdge(d, c, 40); graph.AddEdge(b, d, 50); // Act var actual1 = graph.FindMaxWeightedPath(a, c); var actual2 = graph.FindMaxWeightedPath(c, a); // Assert Assert.Equal(100, actual1); Assert.Equal(100, actual2); }
public void FindShortestPathForSimpleUndirectedGraphUsingDijkstraAlgorithm() { var graph = new UndirectedGraph <object, Edge <object> >(true); object v1 = "vertex1"; object v2 = "vertex2"; object v3 = "vertex3"; var e1 = new Edge <object>(v1, v2); var e2 = new Edge <object>(v2, v3); var e3 = new Edge <object>(v3, v1); graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddEdge(e1); graph.AddEdge(e2); graph.AddEdge(e3); var algorithm = new UndirectedDijkstraShortestPathAlgorithm <object, Edge <object> >(graph, edge => (double)1); var observer = new UndirectedVertexPredecessorRecorderObserver <object, Edge <object> >(); using (observer.Attach(algorithm)) { algorithm.Compute(v1); } IEnumerable <Edge <object> > path; observer.TryGetPath(v3, out path); foreach (var edge in path) { Console.WriteLine(edge); } }
public void Return_30_Given_Graph_With_3_Vertexex_And_3_Edges() { // Arrange var graph = new UndirectedGraph(); var a = new Vertex("A"); var b = new Vertex("B"); var c = new Vertex("C"); graph.AddVertex(a); graph.AddVertex(b); graph.AddVertex(c); graph.AddEdge(a, b, 10); graph.AddEdge(b, c, 20); graph.AddEdge(c, a, 30); // Act var actual1 = graph.FindMaxWeightedPath(a, c); var actual2 = graph.FindMaxWeightedPath(c, a); // Assert Assert.Equal(30, actual1); Assert.Equal(30, actual2); }
public void FindShortestPathForSimpleUndirectedGraphUsingDijkstraAlgorithm() { var graph = new UndirectedGraph<object, Edge<object>>(true); object v1 = "vertex1"; object v2 = "vertex2"; object v3 = "vertex3"; var e1 = new Edge<object>(v1, v2); var e2 = new Edge<object>(v2, v3); var e3 = new Edge<object>(v3, v1); graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddEdge(e1); graph.AddEdge(e2); graph.AddEdge(e3); var algorithm = new UndirectedDijkstraShortestPathAlgorithm<object, Edge<object>>(graph, edge => (double)1); var observer = new UndirectedVertexPredecessorRecorderObserver<object, Edge<object>>(); using (observer.Attach(algorithm)) { algorithm.Compute(v1); } IEnumerable<Edge<object>> path; observer.TryGetPath(v3, out path); foreach (var edge in path) { Console.WriteLine(edge); } }
public void Test_NormalGraph() { var printer = new GraphPrinter(); var graph1 = new UndirectedGraph(); var vertexA = new Vertex("A"); var vertexB = new Vertex("B"); var vertexC = new Vertex("C"); var vertexD = new Vertex("D"); var vertexE = new Vertex("E"); graph1.AddEdge(new UndirectedEdge(vertexA, vertexB)); graph1.AddEdge(new UndirectedEdge(vertexA, vertexC)); graph1.AddEdge(new UndirectedEdge(vertexB, vertexC)); graph1.AddEdge(new UndirectedEdge(vertexB, vertexD)); graph1.AddEdge(new UndirectedEdge(vertexC, vertexE)); graph1.AddEdge(new UndirectedEdge(vertexD, vertexE)); graph1.AddVertex(vertexA); graph1.AddVertex(vertexB); graph1.AddVertex(vertexC); graph1.AddVertex(vertexD); graph1.AddVertex(vertexE); Assert.AreEqual("({A; B; C; D; E}, {(A, B); (A, C); (B, C); (B, D); (C, E); (D, E)})", printer.GraphToString(graph1), "Ошибка: граф №1, GraphToString"); Assert.AreEqual("{(A, B); (A, C); (B, C); (B, D); (C, E); (D, E)}", printer.EdgesToString(graph1), "Ошибка: граф №1, EdgesToString"); Assert.AreEqual("{A; B; C; D; E}", printer.VerticesToString(graph1), "Ошибка: граф №1, VerticesToString"); }
public void TestContainsEdgeWithEdgeTypeReverseEdgeTypeEdge() { var g = new UndirectedGraph <int, Edge <int> >(true); g.AddVertex(1); g.AddVertex(2); g.AddEdge(new Edge <int>(1, 2)); Assert.IsTrue(g.ContainsEdge(new Edge <int>(1, 2))); }
public void TestContainsEdgeWithEdgeWithNoSourceNode() { var g = new UndirectedGraph <int, Edge <int> >(true); g.AddVertex(1); g.AddVertex(2); g.AddEdge(new Edge <int>(1, 2)); Assert.IsFalse(g.ContainsEdge(new Edge <int>(3, 1))); }
public void Should_add_vertex() { Graph graph2 = new UndirectedGraph(); graph2.AddVertex("Paddington"); graph2.AddVertex("Old Street"); Assert.AreEqual("Old Street", graph2.MyGraph[1].VertexID); }
public void TestContainsEdgeCountFalseParallelEdges() { var g = new UndirectedGraph <int, Edge <int> >(false); g.AddVertex(1); g.AddVertex(2); g.AddEdge(new Edge <int>(1, 2)); g.AddEdge(new Edge <int>(2, 1)); Assert.IsTrue(g.EdgeCount == 1); }
public void RemoveVertexIfTest() { var g = new UndirectedGraph <int, IEdge <int> >(); g.AddVertex(0); g.AddVertex(1); int result = g.RemoveVertexIf((vertex) => true); Assert.AreEqual(result, 2); }
public void VertexColoringNoEdge() { /* * (1) * * Generate empty graph: (0) (3) (4) * * (2) */ UndirectedGraph <char, Edge <char> > graph = CreateTestGraph(); var algorithm = new VertexColoringAlgorithm <char, Edge <char> >(graph); algorithm.Compute(); IDictionary <char, int?> coloredVertices = algorithm.Colors; // Graph doesn't have first vertex color Assert.IsFalse(coloredVertices.Values.Contains(1)); int?[] result = coloredVertices.Values.ToArray(); // Expecting to get only 1 color Assert.AreEqual(1, result.Max() + 1); // Not equal to null foreach (int?color in result) { Assert.AreNotEqual(null, color); } // and corresponding colors of vertices Assert.AreEqual(0, result[0]); // 0 vertex = 0 color Assert.AreEqual(0, result[1]); // 1 vertex = 0 color Assert.AreEqual(0, result[2]); // 2 vertex = 0 color Assert.AreEqual(0, result[3]); // 3 vertex = 0 color Assert.AreEqual(0, result[4]); // 4 vertex = 0 color #region Local function UndirectedGraph <char, Edge <char> > CreateTestGraph() { var g = new UndirectedGraph <char, Edge <char> >(true); g.AddVertex('0'); // 1 Vertex g.AddVertex('1'); // 2 Vertex g.AddVertex('2'); // 3 Vertex g.AddVertex('3'); // 4 Vertex g.AddVertex('4'); // 5 Vertex return(g); } #endregion }
public void TestTargetAddEdge() { UndirectedGraph target = new UndirectedGraph(); target.AddVertex("v1"); target.AddVertex("v2"); Assert.IsTrue(target.ContainsVertex("v1")); Assert.IsTrue(target.ContainsVertex("v2")); target.AddEdge("v1", "v2"); Assert.IsTrue(target.ContainsEdge("v1", "v2")); Assert.IsTrue(target.ContainsEdge("v2", "v1")); }
public void AddVertexTest() { var graph = new UndirectedGraph(); var newVertex = new Vertex("test"); Assert.DoesNotThrow(() => graph.AddVertex(newVertex)); Assert.AreEqual(graph.VerticesCount, 1); Assert.AreEqual(graph.Vertices.Count, 1); Assert.AreEqual(graph.Vertices.First(), newVertex); Assert.Throws <InvalidOperationException>(() => graph.AddVertex(newVertex)); Assert.Throws <ArgumentNullException>(() => graph.AddVertex(null)); }
/// <summary> /// DFS is applied to traverse graph with color for bipartite /// </summary> /// <param name="n">N.</param> /// <param name="subGraph" /> /// <param name="maxHash"></param> /// <param name="colorDictionary"></param> private bool DfsSearch(UndirectedGraph <IHash, Edge <IHash> > n, UndirectedGraph <IHash, Edge <IHash> > subGraph, ref IHash maxHash, Dictionary <IHash, int> colorDictionary) { //stack Stack <IHash> stack = new Stack <IHash>(); stack.Push(maxHash); subGraph.AddVertex(maxHash); int color = 1; colorDictionary[maxHash] = color; bool res = true; while (stack.Count > 0) { IHash cur = stack.Pop(); maxHash = n.AdjacentDegree(maxHash) > n.AdjacentDegree(cur) ? maxHash : cur; //opposite color color = colorDictionary[cur] * -1; foreach (var edge in n.AdjacentEdges(cur)) { IHash nei = edge.Source == cur ? edge.Target : edge.Source; //color check if (colorDictionary.Keys.Contains(nei)) { if (colorDictionary[nei] != color) { res = false; } } else { //add vertex subGraph.AddVertex(nei); colorDictionary.Add(nei, color); stack.Push(nei); } //add edge if (!subGraph.ContainsEdge(edge)) { subGraph.AddEdge(edge); } } } return(res); }
public void UndirectedGraph() { var g = new UndirectedGraph <int, Edge <int> >(); //populate g.AddVertex(0); g.AddVertex(1); g.AddEdge(new Edge <int>(0, 1)); var result = SerializeDeserialize <int, Edge <int>, UndirectedGraph <int, Edge <int> > >(g); AssertGraphsEqual(g, result); }
public void TestEdgeCountAndVerticeCountAfterAdd() { var g = new UndirectedGraph <int, Edge <int> >(false); g.AddVertex(1); g.AddVertex(2); g.AddEdge(new Edge <int>(1, 1)); g.AddEdge(new Edge <int>(2, 2)); g.AddEdge(new Edge <int>(1, 2)); bool ret = g.AddVertex(3); Assert.IsTrue(g.EdgeCount == 3 && g.VertexCount == 3); }
public void removeAdjacentEdgeIfTest() { var graph = new UndirectedGraph <int, IEdge <int> >(); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddEdge(new EquatableEdge <int>(1, 2)); graph.AddEdge(new EquatableEdge <int>(1, 3)); graph.RemoveAdjacentEdgeIf(1, (edge) => edge.Target == 2); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(1, 2)); }
public void UndirectedGraph() { var graph = new UndirectedGraph <int, EquatableEdge <int> >(); // Populate graph.AddVertex(0); graph.AddVertex(1); graph.AddEdge(new EquatableEdge <int>(0, 1)); UndirectedGraph <int, EquatableEdge <int> > result = SerializeDeserialize <int, EquatableEdge <int>, UndirectedGraph <int, EquatableEdge <int> > >(graph); AssertGraphsEqual(graph, result); }
private InputModel <char, Edge <char> > GenerateInputTest() { var g = new UndirectedGraph <char, Edge <char> >(true); g.AddVertex('0'); // 1 Vertex g.AddVertex('1'); // 2 Vertex g.AddVertex('2'); // 3 Vertex g.AddVertex('3'); // 4 Vertex g.AddVertex('4'); // 5 Vertex g.AddVertex('5'); // 6 Vertex g.AddVertex('6'); // 7 Vertex g.AddVertex('7'); // 8 Vertex g.AddEdge(new Edge <char>('0', '4')); // 1 Edge g.AddEdge(new Edge <char>('1', '2')); // 2 Edge g.AddEdge(new Edge <char>('1', '3')); // 3 Edge g.AddEdge(new Edge <char>('2', '4')); // 4 Edge g.AddEdge(new Edge <char>('3', '4')); // 5 Edge g.AddEdge(new Edge <char>('5', '7')); // 6 Edge g.AddEdge(new Edge <char>('7', '0')); // 7 Edge return(new InputModel <char, Edge <char> > { Graph = g }); }
private InputModel <char, Edge <char> > GenerateInputEmpty() { var g = new UndirectedGraph <char, Edge <char> >(true); g.AddVertex('0'); // 1 Vertex g.AddVertex('1'); // 2 Vertex g.AddVertex('2'); // 3 Vertex g.AddVertex('3'); // 4 Vertex g.AddVertex('4'); // 5 Vertex return(new InputModel <char, Edge <char> > { Graph = g }); }
public static void Main() { var graph = new UndirectedGraph<int, Edge<int>>(); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddVertex(4); graph.AddEdge(new Edge<int>(1, 2)); graph.AddEdge(new Edge<int>(3, 4)); graph.AddEdge(new Edge<int>(1, 4)); foreach (var edge in graph.Edges) { Console.WriteLine(edge.Source + " " + edge.Target); } }
// effects: Given a list of cells, segments them into multiple // "groups" such that view generation (including validation) of one // group can be done independently of another group. Returns the // groups as a list (uses the foreign key information as well) internal List<CellGroup> GroupRelatedCells() { // If two cells share the same C or S, we place them in the same group // For each cell, determine the Cis and Sis that it refers // to. For every Ci (Si), keep track of the cells that Ci is // contained in. At the end, run through the Cis and Sis and do a // "connected components" algorithm to determine partitions var extentGraph = new UndirectedGraph<EntitySetBase>(EqualityComparer<EntitySetBase>.Default); var extentToCell = new Dictionary<EntitySetBase, Set<Cell>>(EqualityComparer<EntitySetBase>.Default); foreach (var cell in m_cells) { foreach (var extent in new[] { cell.CQuery.Extent, cell.SQuery.Extent }) { Set<Cell> cellsWithExtent; if (!extentToCell.TryGetValue(extent, out cellsWithExtent)) { extentToCell[extent] = cellsWithExtent = new Set<Cell>(); } cellsWithExtent.Add(cell); extentGraph.AddVertex(extent); } extentGraph.AddEdge(cell.CQuery.Extent, cell.SQuery.Extent); var associationSetExtent = cell.CQuery.Extent as AssociationSet; if (associationSetExtent != null) { foreach (var end in associationSetExtent.AssociationSetEnds) { extentGraph.AddEdge(end.EntitySet, associationSetExtent); } } } foreach (var fk in m_foreignKeyConstraints) { extentGraph.AddEdge(fk.ChildTable, fk.ParentTable); } var groupMap = extentGraph.GenerateConnectedComponents(); var result = new List<CellGroup>(); foreach (var setNum in groupMap.Keys) { var cellSets = groupMap.ListForKey(setNum).Select(e => extentToCell[e]); var component = new CellGroup(); foreach (var cellSet in cellSets) { component.AddRange(cellSet); } result.Add(component); } return result; }
public static Pixel GetOrAddVertex(int x, int y, UndirectedGraph<Pixel, TaggedUndirectedEdge<Pixel, EdgeTag>> graph) { Pixel p = VertexSearch(x, y, graph); if (p == null) { p = new Pixel(x, y, Color.Blue); graph.AddVertex(p); } return p; }
public static void Main() { // ot Manage NuGet Packages- izpolzvame vanshna biblioteka; // https://quickgraph.codeplex.com/documentation var graph = new UndirectedGraph<int, Edge<int>>(); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddVertex(4); graph.AddEdge(new Edge<int>(1, 2)); graph.AddEdge(new Edge<int>(3, 4)); graph.AddEdge(new Edge<int>(1, 4)); foreach (var edge in graph.Edges) { Console.WriteLine(edge.Source + " " + edge.Target); } }
static void Main(string[] args) { UndirectedGraph<int, Edge<int>> graph = new UndirectedGraph<int, Edge<int>>(true); // Add vertices to the graph graph.AddVertex(0); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddVertex(4); graph.AddVertex(5); // Create the edges Edge<int> e0_1 = new Edge<int>(0, 1); Edge<int> e0_2 = new Edge<int>(0, 2); Edge<int> e1_2 = new Edge<int>(1, 2); Edge<int> e3_4 = new Edge<int>(3, 4); Edge<int> e4_5 = new Edge<int>(4, 5); Edge<int> e3_5 = new Edge<int>(3, 5); // Add the edges graph.AddEdge(e0_1); graph.AddEdge(e0_2); graph.AddEdge(e1_2); graph.AddEdge(e3_4); graph.AddEdge(e4_5); graph.AddEdge(e3_5); List<int> path = new List<int>(); HamiltonianDefiner definer = new HamiltonianDefiner(graph); bool isHamiltonian = definer.isHamiltonianGraph(path); Console.WriteLine(isHamiltonian); Console.ReadLine(); }
// effects: Given a list of cells, segments them into multiple // "groups" such that view generation (including validation) of one // group can be done independently of another group. Returns the // groups as a list (uses the foreign key information as well) internal List<CellGroup> GroupRelatedCells() { // If two cells share the same C or S, we place them in the same group // For each cell, determine the Cis and Sis that it refers // to. For every Ci (Si), keep track of the cells that Ci is // contained in. At the end, run through the Cis and Sis and do a // "connected components" algorithm to determine partitions // Now form a graph between different cells -- then compute the connected // components in it var graph = new UndirectedGraph<Cell>(EqualityComparer<Cell>.Default); var alreadyAddedCells = new List<Cell>(); // For each extent, add an edge between it and all previously // added extents with which it overlaps foreach (var cell in m_cells) { graph.AddVertex(cell); // Add an edge from this cell to the already added cells var firstCExtent = cell.CQuery.Extent; var firstSExtent = cell.SQuery.Extent; foreach (var existingCell in alreadyAddedCells) { var secondCExtent = existingCell.CQuery.Extent; var secondSExtent = existingCell.SQuery.Extent; // Add an edge between cell and existingCell if // * They have the same C or S extent // * They are linked via a foreign key between the S extents // * They are linked via a relationship var sameExtent = secondCExtent.Equals(firstCExtent) || secondSExtent.Equals(firstSExtent); var linkViaForeignKey = OverlapViaForeignKeys(cell, existingCell); var linkViaRelationship = AreCellsConnectedViaRelationship(cell, existingCell); if (sameExtent || linkViaForeignKey || linkViaRelationship) { graph.AddEdge(existingCell, cell); } } alreadyAddedCells.Add(cell); } // Now determine the connected components of this graph var result = GenerateConnectedComponents(graph); return result; }
static void Main(string[] args) { UndirectedGraph<int> lGraph = new UndirectedGraph<int>(); lGraph.AddVertex(1); lGraph.AddVertex(2); lGraph.AddVertex(3); lGraph.AddVertex(4); lGraph.AddVertex(5); lGraph.AddVertex(6); lGraph.AddVertex(7); lGraph.AddVertex(8); lGraph.Connect(lGraph[1], lGraph[2]); lGraph.Connect(lGraph[2], lGraph[3]); lGraph.Connect(lGraph[7], lGraph[3]); lGraph.Connect(lGraph[5], lGraph[7]); lGraph.Connect(lGraph[2], lGraph[6]); lGraph.Connect(lGraph[5], lGraph[4]); lGraph.Connect(lGraph[4], lGraph[3]); lGraph.Connect(lGraph[7], lGraph[8]); lGraph.Connect(lGraph[7], lGraph[1]); foreach (var lVertex in lGraph.GetVertices()) { Console.WriteLine("Stupeň uzlu {0} = {1}",lVertex.Value, lGraph.GetVertexDegree(lVertex)); } ISequence<int> lSeq = new Sequence<int>(); lSeq.MakeSequence(new Edge<int>(8, 2), new Edge<int>(2, 5), new Edge<int>(5, 1), new Edge<int>(1, 3)); //Console.WriteLine("Is Closed Sequence: " + lSeq.IsClosedSquence()); Console.WriteLine("Is Graph Sequence: " + lSeq.IsGraphSequence(lGraph)); Console.WriteLine("Is Move: " + lSeq.IsMove()); Console.WriteLine("Is Path: " + lSeq.IsPath()); foreach (var lVertex in lGraph.GetVertices()) { var lSousedi = lGraph.GetVertices(lVertex); Console.Write("Sousedé uzlu {0} = ", lVertex.Value); foreach (var nVertex in lSousedi) { Console.Write("{0}, ", nVertex.Value); } Console.WriteLine(""); } var lGr = new UndirectedGraph<int>(); lGr.AddVertex(18); lGr.AddVertex(20); lGr.AddVertex(13); lGr.AddVertex(11); lGr.AddVertex(20); lGr.Connect(lGr[13], lGr[11]); lGr.Connect(lGr[20], lGr[11]); lGr.Connect(lGr[13], lGr[18]); lGr.Connect(lGr[18], lGr[13]); lGr.Connect(lGr[11], lGr[18]); lGr.Connect(lGr[20], lGr[13]); lGraph.Join(lGr, new Edge<int>(1, 11)); foreach (var lVertex in lGraph.GetVertices()) { var lSousedi = lGraph.GetVertices(lVertex); Console.Write("Sousedé uzlu {0} = ", lVertex.Value); foreach (var nVertex in lSousedi) { Console.Write("{0}, ", nVertex.Value); } Console.WriteLine(""); } var lGraf = new UndirectedValuedGraph<int>(); lGraf.AddVertex(1); lGraf.AddVertex(2); lGraf.AddVertex(3); lGraf.AddVertex(4); lGraf.AddVertex(5); lGraf.AddVertex(6); lGraf.Connect(lGraf[1], lGraf[2], 2); lGraf.Connect(lGraf[1], lGraf[3], 2); lGraf.Connect(lGraf[1], lGraf[4], 1); lGraf.Connect(lGraf[1], lGraf[5], 2); lGraf.Connect(lGraf[1], lGraf[6], 3); lGraf.Connect(lGraf[2], lGraf[3], 3); lGraf.Connect(lGraf[2], lGraf[4], 4); lGraf.Connect(lGraf[2], lGraf[5], 1); lGraf.Connect(lGraf[2], lGraf[6], 3); lGraf.Connect(lGraf[3], lGraf[4], 1); lGraf.Connect(lGraf[3], lGraf[5], 2); lGraf.Connect(lGraf[3], lGraf[6], 1); lGraf.Connect(lGraf[4], lGraf[5], 2); lGraf.Connect(lGraf[4], lGraf[6], 3); lGraf.Connect(lGraf[5], lGraf[6], 3); var lVys = lGraf.GetSpanningTree(); var lSeznam = lVys.WideSearch(lGraf[6]); Console.ReadKey(); }
private void Initialize() { _graph = new UndirectedGraph<PointVertex, Edge<PointVertex>>(); _nodes.Clear(); foreach (Obstacle obstacle in _obstacles) { foreach (RotationTreeNode node in obstacle.Nodes) { _nodes.Add(node); _graph.AddVertex(new PointVertex(node.Point)); } _graph.AddEdgeRange(obstacle.Segments.Select(s => new Edge<PointVertex>(new PointVertex(s.Point1.Point), new PointVertex(s.Point2.Point)))); } foreach (Point point in _singlePoints) { Obstacle obstacle = _obstacles.FirstOrDefault(o => o.Contains(point)); var newPoint = new RotationTreeNode(obstacle, point, true); _graph.AddVertex(new PointVertex(point)); _nodes.Add(newPoint); } double maxX = _nodes.Max(p => p.Point.X); _plusInf = new RotationTreeNode(new Point(maxX + 100, double.PositiveInfinity)); _minusInf = new RotationTreeNode(new Point(maxX + 100, double.NegativeInfinity)); _plusInf.AddChild(_minusInf); foreach (RotationTreeNode node in _nodes.OrderByDescending(n => n)) _minusInf.AddChild(node); }
public void test6() { UndirectedGraph<int, Edge<int>> graph = new UndirectedGraph<int, Edge<int>>(true); // Add vertices to the graph graph.AddVertex(0); graph.AddVertex(1); // Create the edges Edge<int> e0_1 = new Edge<int>(0, 1); // Add the edges graph.AddEdge(e0_1); List<int> path = new List<int>(); HamiltonianDefiner definer = new HamiltonianDefiner(graph); bool isHamiltonian = definer.isHamiltonianGraph(path); Assert.AreEqual(isHamiltonian, false); }
public void InitializeGraph(String goPath) { AdjacencyGraph<Int32, Edge<Int32>> graph = null; String line, goNamespace = null; Int32 currentTerm = 0; Int32 parentTerm = 0; using(StreamReader reader = new StreamReader(goPath)) { while (!reader.EndOfStream) { line = reader.ReadLine(); if (line.StartsWith("[Typedef]")) { reader.ReadToEnd(); } else if (line.StartsWith("id:")) { currentTerm = Int32.Parse(line.Substring(7,7)); } else if (line.StartsWith("namespace:")) { goNamespace = line.Substring(11); } else if (line.StartsWith("is_a:")) { if (!graphs.ContainsKey(goNamespace)) { graphs.Add(goNamespace, new AdjacencyGraph<Int32, Edge<Int32>>()); } graph = graphs[goNamespace]; graph.AddVertex(currentTerm); parentTerm = Int32.Parse(line.Substring(10,7)); graph.AddVertex(parentTerm); graph.AddEdge(new Edge<Int32>(currentTerm,parentTerm)); } } } foreach(String ns in graphs.Keys) { var g = graphs[ns]; var newGraph = new UndirectedGraph<Int32, UndirectedEdge<Int32>>(); foreach (Edge<Int32> e in g.Edges) { newGraph.AddVertex(e.Source); newGraph.AddVertex(e.Target); newGraph.AddEdge(new UndirectedEdge<Int32>(e.Source, e.Target)); } undirectedGraphs.Add(ns, newGraph); } }
public static void createTables() { // Create graph var graph = new UndirectedGraph<int, Edge<int>>(); // Add vertices to the graph for (var i = 0; i < FormMain.nodes.Count; i++) graph.AddVertex(i); // Create edges var edgeCost = new Dictionary<Edge<int>, double>(); for (var i = 0; i < FormMain.edges.Count; i++) { var quickGraphEdge = new Edge<int>(FormMain.nodes.IndexOf(FormMain.edges[i].nodes[0]), FormMain.nodes.IndexOf(FormMain.edges[i].nodes[1])); graph.AddEdge(quickGraphEdge); edgeCost.Add(quickGraphEdge, FormMain.edges[i].Weight); } // Initialize tables distanceTable = new double[FormMain.nodes.Count, FormMain.nodes.Count]; pathTable = new List<int>[FormMain.nodes.Count, FormMain.nodes.Count]; for (var i = 0; i < FormMain.nodes.Count; i++) for (var j = 0; j < FormMain.nodes.Count; j++) { distanceTable[i, j] = double.PositiveInfinity; pathTable[i, j] = new List<int>(); } // Create tables for (var source = 0; source < FormMain.nodes.Count; source++) { // We want to use Dijkstra on this graph var dijkstra = new UndirectedDijkstraShortestPathAlgorithm<int, Edge<int>>(graph, edge => edgeCost[edge]); // attach a distance observer to give us the shortest path distances var distObserver = new UndirectedVertexDistanceRecorderObserver<int, Edge<int>>(edge => edgeCost[edge]); distObserver.Attach(dijkstra); // Attach a Vertex Predecessor Recorder Observer to give us the paths var predecessorObserver = new UndirectedVertexPredecessorRecorderObserver<int, Edge<int>>(); predecessorObserver.Attach(dijkstra); // Run the algorithm for current Node dijkstra.Compute(source); // Add values to table foreach (var target in distObserver.Distances) { distanceTable[source, target.Key] = target.Value; IEnumerable<Edge<int>> path; if (predecessorObserver.TryGetPath(target.Key, out path)) pathTable[source, target.Key].AddRange(path.Select(edge => edge.Source).Concat(path.Select(edge => edge.Target)).Distinct().OrderBy(next => distObserver.Distances[next])); } } // Create route tables foreach (var source in FormMain.nodes) source.routeTable = FormMain.nodes.ToDictionary(target => target, target => ( from edge in FormMain.edges where edge.nodes.Contains(source) let adjacent = edge.nodes[0] != source ? edge.nodes[0] : edge.nodes[1] where !pathTable[adjacent.ID, target.ID].Contains(source.ID) orderby edge.Weight + distanceTable[adjacent.ID, target.ID] ascending select adjacent ).ToList()); TablesRequired = false; }