/// <summary> /// Creates a specific diagram and highlight the longest path found through the graph analysis. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void LongestPath(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary <Node, RadDiagramShape> nodeMap; Dictionary <Edge, RadDiagramConnection> edgeMap; // this creates the specific graph var g = this.diagram.CreateDiagram(new List <string> { "1,2", "2,3", "2,4", "3,5", "4,5", "5,6", "2,7", "7,8", "8,9", "9,10", "10,6", "20,21", "21,22", "20,25" }, out nodeMap, out edgeMap, GraphExtensions.CreateShape, this.RandomSizeCheck.IsChecked.HasValue && this.RandomSizeCheck.IsChecked.Value); // note that this works on disconnected graphs as well as on connected ones var path = g.FindLongestPath(); // highlight the longest path if one is found if (path != null) { this.Highlight(path, nodeMap, edgeMap, this.HighlighBrush); } // use a layout which displays the result best diagram.Layout(LayoutType.Tree, new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeUp, VerticalSeparation = 80d, HorizontalSeparation = 50d }); }
/// <summary> /// Creates a random connected graph and displayes a (non-unique) spanning tree using Prim's algorithm. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void Prims(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary <Node, RadDiagramShape> nodeMap; Dictionary <Edge, RadDiagramConnection> edgeMap; var randomConnectedGraph = GraphExtensions.CreateRandomConnectedGraph(10); var root = randomConnectedGraph.FindTreeRoot(); var g = diagram.CreateDiagram(randomConnectedGraph, out nodeMap, out edgeMap, GraphExtensions.CreateShape, specs.RandomShapeSize); // making it undirected will reach all the nodes since the random graph is connected g.IsDirected = false; var tree = g.PrimsSpanningTree(root); if (tree != null) { this.Highlight(tree, nodeMap, edgeMap, this.HighlighBrush); } var settings = new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeDown, VerticalSeparation = 50d, HorizontalSeparation = 80d, }; diagram.Layout(LayoutType.Tree, settings); }
/// <summary> /// Creates a specific graph which has some obvious cycles and lets the graph analysis highlight them, thus confirming the cycles /// which can be easily found manually. The analysis goes of course beyond what the human eye can see and would find cycles in an arbitrary graph. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void Cycles(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary <Node, RadDiagramShape> nodeMap; Dictionary <Edge, RadDiagramConnection> edgeMap; var g = diagram.CreateDiagram(new List <string> { "1,2", "3,1", "2,4", "4,3", "4,5", "10,11", "11,12", "12,10" }, out nodeMap, out edgeMap, specs.CreateShape, specs.RandomShapeSize); var cycles = g.FindCycles(); if (cycles.Count > 0) { foreach (var cycle in cycles) { var path = new GraphPath <Node, Edge>(); cycle.ToList().ForEach(path.AddNode); this.Highlight(path, nodeMap, edgeMap, specs.HighlightBrush); } } diagram.Layout(LayoutType.Tree, new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeRight, VerticalSeparation = 50d, HorizontalSeparation = 80d }); }
/// <summary> /// Creates a random connected graph and displayes a (non-unique) spanning tree using Prim's algorithm. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void Prims(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary<Node, RadDiagramShape> nodeMap; Dictionary<Edge, RadDiagramConnection> edgeMap; var randomConnectedGraph = GraphExtensions.CreateRandomConnectedGraph(10); var root = randomConnectedGraph.FindTreeRoot(); var g = diagram.CreateDiagram(randomConnectedGraph, out nodeMap, out edgeMap, GraphExtensions.CreateShape, specs.RandomShapeSize); // making it undirected will reach all the nodes since the random graph is connected g.IsDirected = false; var tree = g.PrimsSpanningTree(root); if (tree != null) this.Highlight(tree, nodeMap, edgeMap, this.HighlighBrush); var settings = new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeDown, VerticalSeparation = 50d, HorizontalSeparation = 80d, }; diagram.Layout(LayoutType.Tree, settings); }
/// <summary> /// Creates a specific diagram and highlight the longest path found through the graph analysis. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void LongestPath(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary<Node, RadDiagramShape> nodeMap; Dictionary<Edge, RadDiagramConnection> edgeMap; // this creates the specific graph var g = this.diagram.CreateDiagram(new List<string> { "1,2", "2,3", "2,4", "3,5", "4,5", "5,6", "2,7", "7,8", "8,9", "9,10", "10,6", "20,21", "21,22", "20,25" }, out nodeMap, out edgeMap, GraphExtensions.CreateShape, this.RandomSizeCheck.IsChecked.HasValue && this.RandomSizeCheck.IsChecked.Value); // note that this works on disconnected graphs as well as on connected ones var path = g.FindLongestPath(); // highlight the longest path if one is found if (path != null) this.Highlight(path, nodeMap, edgeMap, this.HighlighBrush); // use a layout which displays the result best diagram.Layout(LayoutType.Tree, new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeUp, VerticalSeparation = 80d, HorizontalSeparation = 50d }); }
/// <summary> /// Creates a specific graph which has some obvious cycles and lets the graph analysis highlight them, thus confirming the cycles /// which can be easily found manually. The analysis goes of course beyond what the human eye can see and would find cycles in an arbitrary graph. /// </summary> /// <param name="diagram">The diagram.</param> /// <param name="specs">The specs.</param> private void Cycles(RadDiagram diagram, GraphGenerationSpecifications specs) { diagram.Clear(); Dictionary<Node, RadDiagramShape> nodeMap; Dictionary<Edge, RadDiagramConnection> edgeMap; var g = diagram.CreateDiagram(new List<string> { "1,2", "3,1", "2,4", "4,3", "4,5", "10,11", "11,12", "12,10" }, out nodeMap, out edgeMap, specs.CreateShape, specs.RandomShapeSize); var cycles = g.FindCycles(); if (cycles.Count > 0) { foreach (var cycle in cycles) { var path = new GraphPath<Node, Edge>(); cycle.ToList().ForEach(path.AddNode); this.Highlight(path, nodeMap, edgeMap, specs.HighlightBrush); } } diagram.Layout(LayoutType.Tree, new TreeLayoutSettings { TreeLayoutType = TreeLayoutType.TreeRight, VerticalSeparation = 50d, HorizontalSeparation = 80d }); }