/// <inheritdoc/> public void ApplyLayout(LayoutGraph graph) { var affectedNodesDP = graph.GetDataProvider(AffectedNodesDpKey); for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { Edge e = ec.Edge; YPointPath path = graph.GetPath(e); //adjust source point if (affectedNodesDP == null || affectedNodesDP.GetBool(e.Source)) { AdjustPortLocation(graph, e, path, true); } if (affectedNodesDP == null || affectedNodesDP.GetBool(e.Target)) { AdjustPortLocation(graph, e, path, false); } } }
public static void Main() { //instantiates an empty graph yWorks.Algorithms.Graph graph = new yWorks.Algorithms.Graph(); //create a temporary node array for fast lookup Node[] tmpNodes = new Node[5]; //create some nodes in the graph and store references in the array for (int i = 0; i < 5; i++) { tmpNodes[i] = graph.CreateNode(); } //create some edges in the graph for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { //create an edge from node at index i to node at index j graph.CreateEdge(tmpNodes[i], tmpNodes[j]); } } //output the nodes of the graph Console.WriteLine("The nodes of the graph"); for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next()) { Node node = nc.Node; Console.WriteLine(node); Console.WriteLine("in edges #" + node.InDegree); for (IEdgeCursor ec = node.GetInEdgeCursor(); ec.Ok; ec.Next()) { Console.WriteLine(ec.Edge); } Console.WriteLine("out edges #" + node.OutDegree); for (IEdgeCursor ec = node.GetOutEdgeCursor(); ec.Ok; ec.Next()) { Console.WriteLine(ec.Edge); } } //output the edges of the graph Console.WriteLine("\nThe edges of the graph"); for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { Console.WriteLine(ec.Edge); } //reverse edges that have consecutive neighbors in graph //reversing means switching source and target node for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { if (Math.Abs(ec.Edge.Source.Index - ec.Edge.Target.Index) == 1) { graph.ReverseEdge(ec.Edge); } } Console.WriteLine("\nthe edges of the graph after some edge reversal"); for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { Console.WriteLine(ec.Edge); } /////////////////////////////////////////////////////////////////////////// // Node- and EdgeMap handling /////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// //create a nodemap for the graph INodeMap nodeMap = graph.CreateNodeMap(); foreach (var node in graph.Nodes) { //associate descriptive String to the node via a nodemap nodeMap.Set(node, "this is node " + node.Index); } //create an edgemap for the graph IEdgeMap edgeMap = graph.CreateEdgeMap(); foreach (var edge in graph.Edges) { //associate descriptive String to the edge via an edgemap edgeMap.Set(edge, "this is edge [" + nodeMap.Get(edge.Source) + "," + nodeMap.Get(edge.Target) + "]"); } //output the nodemap values of the nodes Console.WriteLine("\nThe node map values of the graph"); foreach (var node in graph.Nodes) { Console.WriteLine(nodeMap.Get(node)); } //output the edges of the graph Console.WriteLine("\nThe edge map values of the graph"); foreach (var edge in graph.Edges) { Console.WriteLine(edgeMap.Get(edge)); } //cleanup unneeded node and edge maps again (free resources) graph.DisposeNodeMap(nodeMap); graph.DisposeEdgeMap(edgeMap); /////////////////////////////////////////////////////////////////////////// // removing elements from the graph ////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next()) { //remove node that has a edge degree > 2 if (nc.Node.Degree > 2) { //removed the node and all of its adjacent edges from the graph graph.RemoveNode(nc.Node); } } Console.WriteLine("\ngraph after some node removal"); Console.WriteLine(graph); Console.WriteLine("\nPress key to end demo."); Console.ReadKey(); }
/// <summary> /// Main method: /// </summary> public static void Main() { // Create a random graph with the given edge and node count RandomGraphGenerator randomGraph = new RandomGraphGenerator(0); randomGraph.NodeCount = 30; randomGraph.EdgeCount = 60; Graph graph = randomGraph.Generate(); // Create an edgemap and assign random double weights to // the edges of the graph. IEdgeMap weightMap = graph.CreateEdgeMap(); Random random = new Random(0); for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { Edge e = ec.Edge; weightMap.SetDouble(e, 100.0 * random.NextDouble()); } // Calculate the shortest path from the first to the last node // within the graph if (!graph.Empty) { Node from = graph.FirstNode; Node to = graph.LastNode; double sum = 0.0; // The undirected case first, i.e. edges of the graph and the // resulting shortest path are considered to be undirected EdgeList path = ShortestPaths.SingleSourceSingleSink(graph, from, to, false, weightMap); for (IEdgeCursor ec = path.Edges(); ec.Ok; ec.Next()) { Edge e = ec.Edge; double weight = weightMap.GetDouble(e); Console.WriteLine(e + " weight = " + weight); sum += weight; } if (sum == 0.0) { Console.WriteLine("NO UNDIRECTED PATH"); } else { Console.WriteLine("UNDIRECTED PATH LENGTH = " + sum); } // Next the directed case, i.e. edges of the graph and the // resulting shortest path are considered to be directed. // Note that this shortest path can't be shorter then the one // for the undirected case path = ShortestPaths.SingleSourceSingleSink(graph, from, to, true, weightMap); sum = 0.0; for (IEdgeCursor ec = path.Edges(); ec.Ok; ec.Next()) { Edge e = ec.Edge; double weight = weightMap.GetDouble(e); Console.WriteLine(e + " weight = " + weight); sum += weight; } if (sum == 0.0) { Console.WriteLine("NO DIRECTED PATH"); } else { Console.WriteLine("DIRECTED PATH LENGTH = " + sum); } Console.WriteLine("\nAuxiliary distance test\n"); INodeMap distanceMap = graph.CreateNodeMap(); INodeMap predMap = graph.CreateNodeMap(); ShortestPaths.SingleSource(graph, from, true, weightMap, distanceMap, predMap); if (distanceMap.GetDouble(to) == double.PositiveInfinity) { Console.WriteLine("Distance from first to last node is infinite"); } else { Console.WriteLine("Distance from first to last node is " + distanceMap.GetDouble(to)); } // Dispose distanceMap since it is not needed anymore graph.DisposeNodeMap(distanceMap); } // Dispose weightMap since it is not needed anymore graph.DisposeEdgeMap(weightMap); Console.WriteLine("\nPress key to end demo."); Console.ReadKey(); }