Example #1
0
 public void AddVertex(GraphVertex <T> graphVertex)
 {
     if (Vertices.Contains(graphVertex))
     {
         return;
     }
     Vertices.Add(graphVertex);
     NumberOfVertices++;
 }
Example #2
0
        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);
        }
Example #3
0
 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);
 }
Example #4
0
 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);
         }
     }
 }
Example #5
0
        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);
                }
            }
        }
Example #6
0
        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;
            }
        }
Example #7
0
        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--;
        }
Example #8
0
        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);
        }
Example #9
0
        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);
                    }
                }
            }
        }
Example #10
0
        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);
        }
Example #11
0
 public bool HasTwoWayConnectionWith(GraphVertex <T> other)
 {
     return(IsConnectedTo(other) && other.IsConnectedTo(this));
 }
Example #12
0
 public bool IsConnectedTo(GraphVertex <T> other)
 {
     return(Neighbours.Contains(other));
 }
Example #13
0
 public int OuterRank(GraphVertex <T> vertex)
 {
     return(vertex.Neighbours.Count);
 }
Example #14
0
 public bool HasEdge(GraphVertex <T> v1, GraphVertex <T> v2)
 {
     return(v1.IsConnectedTo(v2));
 }
Example #15
0
 public abstract void RemoveEdge(GraphVertex <T> v1, GraphVertex <T> v2);
Example #16
0
 public abstract void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2);
Example #17
0
 public abstract void AddEdge(GraphVertex <T> v1, GraphVertex <T> v2, int cost);
Example #18
0
 public override bool IsTerminal(GraphVertex <T> vertex)
 {
     return(OuterRank(vertex) == 0);
 }
Example #19
0
 public override bool IsTerminal(GraphVertex <T> vertex)
 {
     return(vertex.Neighbours.Count == 1);
 }
Example #20
0
 public override void RemoveEdge(GraphVertex <T> v1, GraphVertex <T> v2)
 {
     v1?.Neighbours.Remove(v2);
     v2?.Neighbours.Remove(v1);
 }
Example #21
0
 public abstract bool IsIsolated(GraphVertex <T> graphVertex);
Example #22
0
 public abstract bool IsTerminal(GraphVertex <T> vertex);
Example #23
0
 public override bool IsIsolated(GraphVertex <T> graphVertex)
 {
     return(graphVertex.Neighbours.Count == 0);
 }
Example #24
0
 public int InnerRank(GraphVertex <T> vertex)
 {
     return(Vertices.Count(graphVertex => graphVertex.IsConnectedTo(vertex)));
 }
Example #25
0
 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);
 }
Example #26
0
 public override bool IsIsolated(GraphVertex <T> vertex)
 {
     return(OuterRank(vertex) + InnerRank(vertex) == 0);
 }