public Dictionary <Vertex, int> BfsShortestPathForDAC(Graph graph, Vertex startVertex)
        {
            Dictionary <Vertex, int> distance       = new Dictionary <Vertex, int>();
            GeneralSamples           generalSamples = new GeneralSamples();

            Queue <Vertex> deque = generalSamples.TopologicalSort(graph);

            distance[startVertex] = 0;

            while (deque.Count > 0)
            {
                Vertex vertex = deque.Dequeue();

                foreach (Edge edge in vertex.Edges)
                {
                    int v1Distance = distance.ContainsKey(edge.Vertex1) ? distance[vertex] : 1000;
                    int v2Distance = distance.ContainsKey(edge.Vertex2) ? distance[vertex] : 1000;

                    if (v2Distance > v1Distance + edge.Weight)
                    {
                        distance[edge.Vertex2] = v1Distance + edge.Weight;
                    }
                }
            }

            return(distance);
        }
Exemplo n.º 2
0
        public static void KnightTourTest()
        {
            GeneralSamples knightTour = new GeneralSamples();

            Console.WriteLine("the solution is");

            //knightTour.solveKnightTour();
        }
Exemplo n.º 3
0
 private static void GeneralSamplesTest()
 {
     GeneralSamples generalSamples = new GeneralSamples();
     //genSamples.NumberOfIslandsTest();
     //genSamples.TopologicalSortTest();
     //genSamples.TarganStronglyConnectedComponentsTest();
     //genSamples.FindLaddersTest();
     //genSamples.GetAllBridgesTest();
 }
Exemplo n.º 4
0
        // Main function to find all cycles
        public List <List <Vertex> > SimpleCyles(Graph graph)
        {
            blockedSet = new HashSet <Vertex>();
            blockedMap = new Dictionary <Vertex, HashSet <Vertex> >();
            stack      = new Stack <Vertex>();
            allCycles  = new List <List <Vertex> >();

            long startIndex = 1;

            GeneralSamples tarjan = new GeneralSamples();

            while (startIndex <= graph.Verticies.Count)
            {
                Graph subGraph = CreateSubGraph(startIndex, graph);
                List <HashSet <Vertex> > sccs = tarjan.TarganStronglyConnectedComponents(subGraph);

                // This creates graph consisting of strongly connected components only and then returns the
                // least indexed vertex among all the strongly connected component graph.
                // It also ignore one vertex graph since it wont have any cycle.

                //Optional<Vertex> maybeLeastVertex = LeastIndexSCC(sccs, subGraph);
                //if (maybeLeastVertex.isPresent())
                Vertex leastVertex = LeastIndexSCC(sccs, subGraph);
                if (leastVertex != null)
                {
                    blockedSet.Clear();
                    blockedMap.Clear();

                    FindCyclesInSCG(leastVertex, leastVertex);
                    startIndex = leastVertex.Id + 1;
                }
                else
                {
                    break;
                }
            }
            return(allCycles);
        }