GraphExtensions
The static graph-analysis related extensions.
Definition
Namespace:Telerik.Windows.Diagrams.Core
Assembly:Telerik.Windows.Diagrams.Core.dll
Syntax:
public static class GraphExtensions
Inheritance: objectGraphExtensions
Methods
Assigns tree-levels to the nodes.
public static void AssignLevel<TLinkData>(this Graph<TreeLayoutData, TLinkData> graph, Node<TreeLayoutData, TLinkData> startNode, Dictionary<Node<TreeLayoutData, TLinkData>, bool> visited = null, int offset = 0) where TLinkData : new()
The graph.
startNodeNode<TreeLayoutData, TLinkData>The start node.
visitedDictionary<Node<TreeLayoutData, TLinkData>, bool>The nodes which have already been visited.
offsetintThe offset.
Performs a BFT of the given graph starting at the given node and stops when the first node matching the condition is found.
public static TNode BreadthFirstSearch<TNode, TLink>(this GraphBase<TNode, TLink> graph, Func<TNode, bool> condition, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph to traverse.
conditionFunc<TNode, bool>The condition a node has to satisfy to be return and thus halt the traversal.
startNodeTNodeThe start node.
Returns:TNode
Performs a breadth-first traversal of the graph starting at the given node.
public static void BreadthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, Action<TNode> action, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph to traverse.
actionAction<TNode>The action acting a the visited node.
startNodeTNodeThe start node.
Performs a breadth-first traversal of the graph starting at the given node.
public static void BreadthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, IVisitor<TNode> visitor, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph to traverse.
visitorIVisitor<TNode>The visitor traversing the graph.
startNodeTNodeThe start node.
Returns a shallow clone from the given collection.
public static IList<Edge<TNodeData, TLinkData>> Clone<TNodeData, TLinkData>(this IEnumerable<Edge<TNodeData, TLinkData>> list) where TNodeData : new() where TLinkData : new()
The collection to clone.
Returns:Creates a bi-directional dictionary with keys equal to the (supposedly unique) identifiers and value equal to the provided initial value.
public static Dictionary<Tuple<TNode, TNode>, int> CreateBiDictionary<TNode, TLink>(this GraphBase<TNode, TLink> graph, int value) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
valueintThe value.
Returns:Dictionary<Tuple<TNode, TNode>, int>
Creates a dictionary with keys equal to the (supposedly unique) identifiers and value equal to the provided initial value.
public static Dictionary<int, int> CreateDictionary<TNode, TLink>(this GraphBase<TNode, TLink> graph, int value) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
valueintThe value.
Returns:Dictionary<int, int>
Creates a random connected graph.
public static GraphBase<Node<object, object>, Edge<object, object>> CreateRandomConnectedGraph(int nodesCount, int maxIncidence = 4, bool tree = false)
The nodes count.
maxIncidenceintThe max incidence.
treeboolIf set to true the random graph will be effectively a tree.
Creates a random graph.
public static GraphBase<Node<object, object>, Edge<object, object>> CreateRandomGraph(int nodesCount = 150, int maxIncidence = 4, bool tree = false)
The count.
maxIncidenceintThe maximum incidence of each node.
treeboolIf set to true the generated graph will be a tree.
Performs a depth-first traversal of the graph starting at the given node.
public static void DepthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, Action<TNode, int> action, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
actionAction<TNode, int>The action.
startNodeTNodeThe start node.
Performs a depth-first traversal of the graph starting at the given node.
public static void DepthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, Action<TNode> action, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
actionAction<TNode>The action.
startNodeTNodeThe start node.
Performs a depth-first traversal of the graph starting at the given node.
public static void DepthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, IDepthVisitor<TNode> visitor, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
visitorIDepthVisitor<TNode>The visitor.
startNodeTNodeThe start node.
Performs a depth-first traversal of the graph starting at the given node.
public static void DepthFirstTraversal<TNode, TLink>(this GraphBase<TNode, TLink> graph, IVisitor<TNode> visitor, TNode startNode) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph to traverse.
visitorIVisitor<TNode>The visitor.
startNodeTNodeThe start node.
Finds cycles in a graph using Tarjan strongly connected components algorithm.
public static IList<TNode[]> FindCycles<TNode, TLink>(this GraphBase<TNode, TLink> graph, bool excludeSingleItems = true) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph.
excludeSingleItemsboolIf set to true nodes with no edges are excluded.
IList<TNode[]>
A list of of vertex arrays (paths) that form cycles in the graph.
Compares the two graph and assert they are identical.
public static bool HasIdenticalStructureWith(this GraphBase<Node<object, object>, Edge<object, object>> graph1, GraphBase<Node<object, object>, Edge<object, object>> graph2)
Kruskal algorithm.
Merges the given graph into the current graph.
public static GraphBase<Node<object, object>, Edge<object, object>> Merge(this GraphBase<Node<object, object>, Edge<object, object>> graph, GraphBase<Node<object, object>, Edge<object, object>> otherGraph)
The graph.
otherGraphGraphBase<Node<object, object>, Edge<object, object>>The graph to merge into the current one.
Returns:Offsets the specified graph.
public static void MoveGraph<TNodeData, TLinkData>(this GraphBase<Node<TNodeData, TLinkData>, Edge<TNodeData, TLinkData>> layoutGraph, double offsetX, double offsetY) where TNodeData : new() where TLinkData : new()
The layout Graph.
offsetXdoubleThe horizontal offset.
offsetYdoubleThe vertical offset.
Moves link.
public static void MoveLink<TNodeData, TLinkData>(this Edge<TNodeData, TLinkData> edge, Point point) where TNodeData : new() where TLinkData : new()
The layout link.
pointPointThe delta to move.
Parses the specified list representing the incidence structure of a graph.
Returns the position of the given rectangle.
public static Point Position(this Rect rect)
The rectangle.
Returns:Point
Prim's algorithm finds a minimum-cost spanning tree of an edge-weighted, connected, undirected graph.
public static GraphBase<TNode, TLink> PrimsSpanningTree<TNode, TLink>(this GraphBase<TNode, TLink> graph, TNode fromNode, bool reverseWrongEdges = false) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
The graph structure.
fromNodeTNodeThe node to start from.
reverseWrongEdgesboolIf set to true and the graph is not directed then the edges which do not point in the correct tree flow direction
will be reversed.
GraphBase<TNode, TLink>
Splits the given, not necessarily connected, graph into its connected components.
public static IEnumerable<Graph<TNodeData, TLinkData>> Split<TNodeData, TLinkData>(this Graph<TNodeData, TLinkData> graph) where TNodeData : new() where TLinkData : new()
The graph to be split.
Returns:IEnumerable<Graph<TNodeData, TLinkData>>
Takes a random node with incidence less than specified.
public static Node<object, object> TakeRandomNode(this GraphBase<Node<object, object>, Edge<object, object>> graph, Node<object, object> node = null, int incidenceLessThan = 4)
The graph.
nodeNode<object, object>The node which should not be returned; i.e. the random node should be in the complement of the given node.
incidenceLessThanintThe incidence less than.
Returns:Takes two random nodes from the given graph.
Executes Tarjan algorithm on the graph.
public static void TarjansStronglyConnectedComponentsAlgorithm<TNode, TLink>(bool excludeSingleItems, TNode node, IDictionary<TNode, int> indices, IDictionary<TNode, int> lowLinks, ICollection<TNode[]> connected, Stack<TNode> stack, int index) where TNode : class, INode<TNode, TLink>, new() where TLink : class, IEdge<TNode, TLink>, new()
If set to true single items (singletons) will not be taken into account.
The node to start with.
indicesIDictionary<TNode, int>The current indices.
lowLinksIDictionary<TNode, int>The current low links.
connectedICollection<TNode[]>The connected components.
stackStack<TNode>The stack.
indexintThe current index.
If the first supplied rectangle has width or height zero the second rectangle will be returned. Otherwise the standard union of two rectangles will be used.
public static Rect UnionEmptyRects(Rect rect1, Rect rect2)
A rectangle.
rect2RectAnother rectangle.
Returns:Rect