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