public void AddVertex(GraphVertex <T> graphVertex) { if (Vertices.Contains(graphVertex)) { return; } Vertices.Add(graphVertex); NumberOfVertices++; }
public void DepthFirstTraversal(GraphVertex <T> initalGraphVertex, Action <GraphVertex <T> > action) { if (initalGraphVertex == null) { return; } var vertexStates = Vertices.ToDictionary(key => key, value => VertexState.NotVisited); DepthFirstTraversalHelper(initalGraphVertex, vertexStates, action); }
public override void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2) { if (!Vertices.Contains(v1) || !Vertices.Contains(v2)) { return; } if (v1.Neighbours.Contains(v2)) { return; } v1.Neighbours.Add(v2); }
private void InitializeDistances(GraphVertex <T> initialGraphVertex, Dictionary <GraphVertex <T>, int> distances, HashSet <GraphVertex <T> > unvisitedSet) { distances.Add(initialGraphVertex, 0); foreach (var vertex in Vertices) { unvisitedSet.Add(vertex); if (!vertex.Equals(initialGraphVertex)) { distances.Add(vertex, Infinity); } } }
private void DepthFirstTraversalHelper(GraphVertex <T> initalGraphVertex, Dictionary <GraphVertex <T>, VertexState> vertexStates, Action <GraphVertex <T> > action) { action(initalGraphVertex); vertexStates[initalGraphVertex] = VertexState.Visited; foreach (var neighbour in initalGraphVertex.Neighbours) { if (vertexStates[neighbour] == VertexState.NotVisited) { DepthFirstTraversalHelper(neighbour, vertexStates, action); } } }
private void Relax(GraphVertex <T> current, GraphVertex <T> neighbour, Dictionary <GraphVertex <T>, int> distancesFromInitialVertex) { if (distancesFromInitialVertex[current] == Infinity) { return; } var newDistance = distancesFromInitialVertex[current] + current.Costs[neighbour]; var oldDistance = distancesFromInitialVertex[neighbour]; if (newDistance < oldDistance) { distancesFromInitialVertex[neighbour] = newDistance; } }
public void RemoveVertex(GraphVertex <T> graphVertex) { if (!Vertices.Contains(graphVertex)) { return; } for (int i = 0; i < graphVertex.Neighbours.Count; i++) { graphVertex.Neighbours[i].Neighbours.Remove(graphVertex); } Vertices.Remove(graphVertex); NumberOfVertices--; }
public Dictionary <GraphVertex <T>, int> Dijkstra(GraphVertex <T> initialGraphVertex) { var unvisitedSet = new HashSet <GraphVertex <T> >(); var visitedSet = new HashSet <GraphVertex <T> >(); var distancesFromInitialVertex = new Dictionary <GraphVertex <T>, int>(); InitializeDistances(initialGraphVertex, distancesFromInitialVertex, unvisitedSet); while (unvisitedSet.Count != 0) { var current = ExtractMin(unvisitedSet, distancesFromInitialVertex); visitedSet.Add(current); foreach (var neighbor in current.Neighbours) { Relax(current, neighbor, distancesFromInitialVertex); } } return(distancesFromInitialVertex); }
public void BreathFirstTraversal(GraphVertex <T> initialGraphVertex, Action <GraphVertex <T> > action) { var vertexStates = Vertices.ToDictionary(key => key, value => VertexState.NotVisited); var queue = new Queue <GraphVertex <T> >(); queue.Enqueue(initialGraphVertex); while (queue.Count > 0) { var currentVertex = queue.Dequeue(); foreach (var neighbor in currentVertex.Neighbours) { if (vertexStates[neighbor] != VertexState.Visited) { vertexStates[neighbor] = VertexState.Visited; action(neighbor); queue.Enqueue(neighbor); } } } }
private void TopologicalSortHelper(GraphVertex <T> graphVertex, HashSet <GraphVertex <T> > stack, Dictionary <GraphVertex <T>, VertexState> vertexStates) { foreach (var neighbor in graphVertex.Neighbours) { if (vertexStates[neighbor] == VertexState.Visited) { continue; } if (vertexStates[neighbor] == VertexState.Visiting) { HasCycle = true; return; } vertexStates[neighbor] = VertexState.Visiting; TopologicalSortHelper(neighbor, stack, vertexStates); vertexStates[neighbor] = VertexState.Visited; } stack.Add(graphVertex); }
public bool HasTwoWayConnectionWith(GraphVertex <T> other) { return(IsConnectedTo(other) && other.IsConnectedTo(this)); }
public bool IsConnectedTo(GraphVertex <T> other) { return(Neighbours.Contains(other)); }
public int OuterRank(GraphVertex <T> vertex) { return(vertex.Neighbours.Count); }
public bool HasEdge(GraphVertex <T> v1, GraphVertex <T> v2) { return(v1.IsConnectedTo(v2)); }
public abstract void RemoveEdge(GraphVertex <T> v1, GraphVertex <T> v2);
public abstract void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2);
public abstract void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2, int cost);
public override bool IsTerminal(GraphVertex <T> vertex) { return(OuterRank(vertex) == 0); }
public override bool IsTerminal(GraphVertex <T> vertex) { return(vertex.Neighbours.Count == 1); }
public override void RemoveEdge(GraphVertex <T> v1, GraphVertex <T> v2) { v1?.Neighbours.Remove(v2); v2?.Neighbours.Remove(v1); }
public abstract bool IsIsolated(GraphVertex <T> graphVertex);
public abstract bool IsTerminal(GraphVertex <T> vertex);
public override bool IsIsolated(GraphVertex <T> graphVertex) { return(graphVertex.Neighbours.Count == 0); }
public int InnerRank(GraphVertex <T> vertex) { return(Vertices.Count(graphVertex => graphVertex.IsConnectedTo(vertex))); }
public override void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2, int cost) { AddEdge(v1, v2); v1.Costs.Add(v2, cost); v2.Costs.Add(v1, cost); }
public override bool IsIsolated(GraphVertex <T> vertex) { return(OuterRank(vertex) + InnerRank(vertex) == 0); }