Graph Traversal Example for Blazor

Nevron NOV Diagram for Blazor ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All Blazor Diagram examples are also available as a stand-alone Visual Studio solution in C#, which you can download from the links below.

Graph Traversal Example for Blazor Description

This example demonstrates the built in support for different graph traversal methods.

There are three graph traversal methods:

  • Depth First Traversal - in this type of graph traversal the algorithm first visits the start vertex and then recursively visits its destination or neighbour vertices (depending on whether you want to treat the graph as directed or undirected).
  • Breadth First Traversal - in this type of graph traversal the algorithm first visits the starting vertex, then all the vertices which can be reached in one step, then in two steps and so on. In Breadth First Traversal you can also treat the graph as directed or undirected.
  • Topological Order Traversal - in this type of graph traversal the algorithm visits the vertices in their topological order. A topological sort is a list of the vertices of a directed graph in which all the successors of any given vertex appear in the sequence after that vertex. Topological Order Traversal is only applicable for directed graphs.

To explore graph traversal just click on a shape to select it. The example will automatically highlight the visited shapes and assign a visiting order to them.


Graph Traversal Example

C# Graph Traversal Example Code for Nevron Diagram for Blazor
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Layout;
using Nevron.Nov.Diagram.Shapes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NGraphTraversalExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
			m_DrawingView = drawingViewWithRibbon.View;

			m_DrawingView.Document.HistoryService.Pause();
			try
			{
				InitDiagram(m_DrawingView.Document);
			}
			finally
			{
				m_DrawingView.Document.HistoryService.Resume();
			}

			return drawingViewWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();

			m_TraversalMethodRadioGroup = new NRadioButtonGroup();
			m_TraversalMethodRadioGroup.FillFromEnum();
			m_TraversalMethodRadioGroup.SelectedIndexChanged += OnRadioGroupSelectedIndexChanged;
			stack.Add(new NGroupBox("Graph Traversal Method:", m_TraversalMethodRadioGroup));

			m_GraphTypeRadioGroup = new NRadioButtonGroup();
			m_GraphTypeRadioGroup.FillFromEnum();
			m_GraphTypeRadioGroup.SelectedIndexChanged += OnRadioGroupSelectedIndexChanged;
			stack.Add(new NGroupBox("Graph Type:", m_GraphTypeRadioGroup));

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates the built in support for different graph traversal methods.

There are three graph traversal methods:

  • Depth First Traversal - in this type of graph traversal the algorithm first visits the start vertex and then recursively visits its destination or neighbour vertices (depending on whether you want to treat the graph as directed or undirected).
  • Breadth First Traversal - in this type of graph traversal the algorithm first visits the starting vertex, then all the vertices which can be reached in one step, then in two steps and so on. In Breadth First Traversal you can also treat the graph as directed or undirected.
  • Topological Order Traversal - in this type of graph traversal the algorithm visits the vertices in their topological order. A topological sort is a list of the vertices of a directed graph in which all the successors of any given vertex appear in the sequence after that vertex. Topological Order Traversal is only applicable for directed graphs.

To explore graph traversal just click on a shape to select it. The example will automatically highlight the visited shapes and assign a visiting order to them.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Shape Navigation", "https://www.nevron.com/docs/openvision/diagram-shape-navigation.html"), ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { NDrawing drawing = drawingDocument.Content; drawing.ScreenVisibility.ShowPorts = false; drawing.ActivePage.Selection.Mode = ENSelectionMode.Single; drawing.ActivePage.Selection.Deselected += OnPageItemDeselected; drawing.ActivePage.Selection.Selected += OnPageItemSelected; // Add a stylesheet for highlighted shapes NStyleSheet styleSheet = new NStyleSheet(); drawingDocument.StyleSheets.Add(styleSheet); NRule rule = styleSheet.CreateRule(sb => { sb.Type(NGeometry.NGeometrySchema); sb.ChildOf(); sb.Type(NShape.NShapeSchema); sb.UserClass(HighlightedUserClassName); }); rule.Declarations.Add(new NValueDeclaration(NGeometry.FillProperty, new NColorFill(NColor.FromARGB(80, 255, 0, 0)))); rule.Declarations.Add(new NValueDeclaration(NGeometry.StrokeProperty, new NStroke(2, NColor.Red))); rule = styleSheet.CreateRule(sb => { sb.Type(NGeometry.NGeometrySchema); sb.ChildOf(); sb.Type(NShape.NShapeSchema); sb.UserClass(NDR.StyleSheetNameConnectors); sb.UserClass(HighlightedUserClassName); }); rule.Declarations.Add(new NValueDeclaration(NGeometry.EndArrowheadProperty, new NArrowhead(ENArrowheadShape.Triangle))); // Create a rectangular grid template NGraphTemplate template = new NRectangularGridTemplate(); template.Origin = new NPoint(10, 23); template.VertexShape = ENBasicShape.Rectangle; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); // Create a tree template template = new NGenericTreeTemplate(); template.Origin = new NPoint(250, 23); template.VertexShape = ENBasicShape.Triangle; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); // Create an elliptical grid template template = new NEllipticalGridTemplate(); template.Origin = new NPoint(10, 250); template.VertexShape = ENBasicShape.Ellipse; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); } private void ClearHighlightedShapes() { // Remove the highligthed user class of all shapes NList shapes = m_DrawingView.ActivePage.GetShapes(false); for (int i = 0; i < shapes.Count; i++) { NShape shape = shapes[i]; shape.RemoveUserClass(HighlightedUserClassName); shape.TextBlock = null; } } private void HighlightShapes() { ClearHighlightedShapes(); // Highlight the selected type of shapes NShape shape = m_DrawingView.ActivePage.Selection.FirstSelected as NShape; if (shape == null || shape.ShapeType == ENShapeType.Shape1D) return; // get the graph in which the shape resides NGraphBuilder graphBuilder = new NGraphBuilder(new NShapeGraphAdapter(), new NGraphPartFactory()); NObjectGraphPartMap objectMap; NGraph graph = graphBuilder.BuildGraph(shape, out objectMap); if (graph == null) return; // get the vertex which represents the shape in the graph NGraphVertex vertex = objectMap.GetPartFromObject(shape) as NGraphVertex; if (vertex == null) return; // create a visitor which will visit and highlight the graph parts NHighlightingVisitor visitor = new NHighlightingVisitor(objectMap); ENGraphType graphType = (ENGraphType)m_GraphTypeRadioGroup.SelectedIndex; switch ((ENGraphTraversalMethod)m_TraversalMethodRadioGroup.SelectedIndex) { case ENGraphTraversalMethod.DepthFirstTraversal: graph.DepthFirstTraversal(graphType, visitor, vertex); break; case ENGraphTraversalMethod.BreadthFirstTraversal: graph.BreadthFirstTraversal(graphType, visitor, vertex); break; case ENGraphTraversalMethod.TopologicalOrderTraversal: graph.TopologicalOrderTraversal(visitor); break; } } #endregion #region Implementation #endregion #region Event Handlers private void OnPageItemDeselected(NItemEventArgs arg) { ClearHighlightedShapes(); } private void OnPageItemSelected(NItemEventArgs arg) { HighlightShapes(); } private void OnRadioGroupSelectedIndexChanged(NValueChangeEventArgs arg) { HighlightShapes(); } #endregion #region Fields private NDrawingView m_DrawingView; private NRadioButtonGroup m_TraversalMethodRadioGroup; private NRadioButtonGroup m_GraphTypeRadioGroup; #endregion #region Static Methods #endregion #region Constants private const string HighlightedUserClassName = "Highlighted"; #endregion #region Nested Types private enum ENGraphTraversalMethod { DepthFirstTraversal, BreadthFirstTraversal, TopologicalOrderTraversal } private class NHighlightingVisitor : NGraphPartVisitor { public NHighlightingVisitor(NObjectGraphPartMap objectMap) { m_ObjectMap = objectMap; } public override bool Visit(NGraphPart part) { if (part is NGraphVertex) { NShape shape = (NShape)m_ObjectMap.GetObjectFromPart(part); shape.Text = m_VertexCount.ToString(); shape.UserClass = HighlightedUserClassName; m_VertexCount++; } return true; } private NObjectGraphPartMap m_ObjectMap; private int m_VertexCount; } #endregion } }

Download a fully functional C# (.cs) Graph Traversal Example for Blazor Solution

Related Documentation Links