コード例 #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
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 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
ファイル: GraphVertex.cs プロジェクト: AlexMSP/Graphs
 public bool HasTwoWayConnectionWith(GraphVertex <T> other)
 {
     return(IsConnectedTo(other) && other.IsConnectedTo(this));
 }
コード例 #12
0
ファイル: GraphVertex.cs プロジェクト: AlexMSP/Graphs
 public bool IsConnectedTo(GraphVertex <T> other)
 {
     return(Neighbours.Contains(other));
 }
コード例 #13
0
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public int OuterRank(GraphVertex <T> vertex)
 {
     return(vertex.Neighbours.Count);
 }
コード例 #14
0
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 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
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public override bool IsTerminal(GraphVertex <T> vertex)
 {
     return(OuterRank(vertex) == 0);
 }
コード例 #19
0
ファイル: UndirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public override bool IsTerminal(GraphVertex <T> vertex)
 {
     return(vertex.Neighbours.Count == 1);
 }
コード例 #20
0
ファイル: UndirectedGraph.cs プロジェクト: AlexMSP/Graphs
 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
ファイル: UndirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public override bool IsIsolated(GraphVertex <T> graphVertex)
 {
     return(graphVertex.Neighbours.Count == 0);
 }
コード例 #24
0
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public int InnerRank(GraphVertex <T> vertex)
 {
     return(Vertices.Count(graphVertex => graphVertex.IsConnectedTo(vertex)));
 }
コード例 #25
0
ファイル: UndirectedGraph.cs プロジェクト: AlexMSP/Graphs
 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
ファイル: DirectedGraph.cs プロジェクト: AlexMSP/Graphs
 public override bool IsIsolated(GraphVertex <T> vertex)
 {
     return(OuterRank(vertex) + InnerRank(vertex) == 0);
 }