Ejemplo n.º 1
0
        /// <summary>
        /// Do DFS to find all unique edges.
        /// </summary>
        private void dfs(WeightedGraphVertex <T, TW> currentVertex, System.Collections.Generic.HashSet <T> visitedVertices, Dictionary <T, System.Collections.Generic.HashSet <T> > visitedEdges,
                         List <MSTEdge <T, TW> > result)
        {
            if (!visitedVertices.Contains(currentVertex.Value))
            {
                visitedVertices.Add(currentVertex.Value);

                foreach (var edge in currentVertex.Edges)
                {
                    if (!visitedEdges.ContainsKey(currentVertex.Value) ||
                        !visitedEdges[currentVertex.Value].Contains(edge.Key.Value))
                    {
                        result.Add(new MSTEdge <T, TW>(currentVertex.Value, edge.Key.Value, edge.Value));

                        //update visited edge
                        if (!visitedEdges.ContainsKey(currentVertex.Value))
                        {
                            visitedEdges.Add(currentVertex.Value, new HashSet <T>());
                        }

                        visitedEdges[currentVertex.Value].Add(edge.Key.Value);

                        //update visited back edge
                        if (!visitedEdges.ContainsKey(edge.Key.Value))
                        {
                            visitedEdges.Add(edge.Key.Value, new HashSet <T>());
                        }

                        visitedEdges[edge.Key.Value].Add(currentVertex.Value);
                    }

                    dfs(edge.Key, visitedVertices, visitedEdges, result);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Do DFS to pick smallest weight neighbour edges
        /// of current spanning tree one by one
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="currentVertex"></param>
        /// <param name="spanTreeVertices"></param>
        /// <param name="spanTreeNeighbours"> Use Fibornacci Min Heap to pick smallest edge neighbour </param>
        /// <param name="spanTreeEdges">result MST edges</param>
        private void DFS(WeightedGraph <T, W> graph,
                         WeightedGraphVertex <T, W> currentVertex,
                         BMinHeap <MSTEdge <T, W> > spanTreeNeighbours,
                         HashSet <T> spanTreeVertices,
                         List <MSTEdge <T, W> > spanTreeEdges)
        {
            //add all edges to Fibornacci Heap
            //So that we can pick the min edge in next step
            foreach (var edge in currentVertex.Edges)
            {
                spanTreeNeighbours.Insert(new MSTEdge <T, W>(
                                              currentVertex.Value,
                                              edge.Key.Value, edge.Value));
            }

            //pick min edge
            var minNeighbourEdge = spanTreeNeighbours.ExtractMin();

            //skip edges already in MST
            while (spanTreeVertices.Contains(minNeighbourEdge.Source) &&
                   spanTreeVertices.Contains(minNeighbourEdge.Destination))
            {
                minNeighbourEdge = spanTreeNeighbours.ExtractMin();

                //if no more neighbours to explore
                //time to end exploring
                if (spanTreeNeighbours.Count == 0)
                {
                    return;
                }
            }

            //keep track of visited vertices
            //do not duplicate vertex
            if (!spanTreeVertices.Contains(minNeighbourEdge.Source))
            {
                spanTreeVertices.Add(minNeighbourEdge.Source);
            }

            //Destination vertex will never be a duplicate
            //since this is an unexplored Vertex
            spanTreeVertices.Add(minNeighbourEdge.Destination);

            //add edge to result
            spanTreeEdges.Add(minNeighbourEdge);

            //now explore the destination vertex
            DFS(graph, graph.Vertices[minNeighbourEdge.Destination],
                spanTreeNeighbours, spanTreeVertices, spanTreeEdges);
        }
Ejemplo n.º 3
0
        public void Base()
        {
            var sut = new CriticalPath();

            var vertex1 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 1,
                        Weight = 5
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 3,
                        Weight = 9
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 4,
                        Weight = 14
                    }
                }
            };

            var vertex2 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 2,
                        Weight = 7
                    }
                }
            };

            var vertex3 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 4,
                        Weight = 4
                    }
                }
            };

            var vertex4 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 4,
                        Weight = 11
                    }
                }
            };

            var vertex5 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>()
            };

            var graph = new [] { vertex1, vertex2, vertex3, vertex4, vertex5 };

            Assert.Equal(20, sut.GetCriticalPath(graph));
        }
Ejemplo n.º 4
0
        public void BiggerGraph()
        {
            var sut = new CriticalPath();

            var vertex1 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 1,
                        Weight = 17
                    }
                }
            };

            var vertex2 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 2,
                        Weight = 13
                    }
                }
            };

            var vertex3 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 3,
                        Weight = 9
                    }
                }
            };

            var vertex4 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 4,
                        Weight = 11
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 8,
                        Weight = 10
                    }
                }
            };

            var vertex5 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                }
            };

            var vertex6 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 6,
                        Weight = 11
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 9,
                        Weight = 15
                    }
                }
            };

            var vertex7 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 7,
                        Weight = 18
                    }
                }
            };

            var vertex8 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 3,
                        Weight = 16
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 8,
                        Weight = 13
                    },
                    new WeightedGraphNodeEdge
                    {
                        To     = 11,
                        Weight = 19
                    }
                }
            };

            var vertex9 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                }
            };

            var vertex10 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 10,
                        Weight = 14
                    },
                }
            };

            var vertex11 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge
                    {
                        To     = 11,
                        Weight = 17
                    },
                }
            };

            var vertex12 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                }
            };

            var graph = new []
            {
                vertex1, vertex2, vertex3, vertex4, vertex5,
                vertex6, vertex7, vertex8, vertex9,
                vertex10, vertex11, vertex12
            };

            Assert.Equal(56, sut.GetCriticalPath(graph));
        }
Ejemplo n.º 5
0
        public void CanCalculateMinDistancesForGraphWithCycles()
        {
            var sut = new FloydWarshall();

            var graph = new WeightedGraphVertex[4];

            var node1 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge {
                        To = 2, Weight = 3
                    },
                    new WeightedGraphNodeEdge {
                        To = 3, Weight = 0
                    },
                }
            };
            var node2 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge {
                        To = 0, Weight = -2
                    },
                    new WeightedGraphNodeEdge {
                        To = 3, Weight = 1
                    },
                }
            };
            var node3 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge {
                        To = 3, Weight = 5
                    },
                }
            };
            var node4 = new WeightedGraphVertex
            {
                Edges = new System.Collections.Generic.List <WeightedGraphNodeEdge>
                {
                    new WeightedGraphNodeEdge {
                        To = 1, Weight = 4
                    },
                }
            };

            graph[0] = node1;
            graph[1] = node2;
            graph[2] = node3;
            graph[3] = node4;

            var(distances, path) = sut.MinDistances(graph);

            Assert.Collection(distances[0],
                              arg => Assert.Equal(0, arg),
                              arg => Assert.Equal(4, arg),
                              arg => Assert.Equal(3, arg),
                              arg => Assert.Equal(0, arg));
            Assert.Collection(distances[1],
                              arg => Assert.Equal(-2, arg),
                              arg => Assert.Equal(0, arg),
                              arg => Assert.Equal(1, arg),
                              arg => Assert.Equal(-2, arg));
            Assert.Collection(distances[2],
                              arg => Assert.Equal(7, arg),
                              arg => Assert.Equal(9, arg),
                              arg => Assert.Equal(0, arg),
                              arg => Assert.Equal(5, arg));
            Assert.Collection(distances[3],
                              arg => Assert.Equal(2, arg),
                              arg => Assert.Equal(4, arg),
                              arg => Assert.Equal(5, arg),
                              arg => Assert.Equal(0, arg));
        }