Exemple #1
0
        public static int[,] FloydAlgo(GraphDS graph)
        {
            int v = graph.numberOfVertices;

            for (int j = 0; j < v; j++)
            {
                for (int k = 0; k < v; k++)
                {
                    if (k == j)
                    {
                        graph.adjMatrix[j, k] = 0;
                    }
                }
            }

            for (int k = 0; k < v; k++)
            {
                for (int i = 0; i < v; i++)
                {
                    for (int j = 0; j < v; j++)
                    {
                        if (graph.adjMatrix[i, j] > graph.adjMatrix[i, k] + graph.adjMatrix[k, j])
                        {
                            graph.adjMatrix[i, j] = graph.adjMatrix[i, k] + graph.adjMatrix[k, j];
                        }
                    }
                }
            }
            return(graph.adjMatrix);
        }
Exemple #2
0
        public static int[] DjikshtraAlgo(string source, GraphDS graph)
        {
            //Need to use priority queue
            // Will update with correct solution soon
            Queue <GraphNode> q = new Queue <GraphNode>();

            q.Enqueue(graph.vertices[graph.GetIndexOfVertex(source)]);
            int[] distance = new int[graph.vertices.Length];
            distance[graph.GetIndexOfVertex(graph.vertices[graph.GetIndexOfVertex(source)].label)] = 0;
            while (q.Count > 0)
            {
                GraphNode current = q.Dequeue();
                current.isVisited = true;
                GraphNode[] adjNodes = graph.GetAdjacentVertex(current.label);
                foreach (var node in adjNodes)
                {
                    if (!node.isVisited)
                    {
                        q.Enqueue(node);
                        node.isVisited = true;
                        distance[graph.GetIndexOfVertex(node.label)] = distance[graph.GetIndexOfVertex(current.label)] + 1;
                    }
                }
            }
            return(distance);
        }
        public static void DepthFirstSearch(string source, GraphDS graph)
        {
            Console.WriteLine(source);

            graph.vertices[graph.GetIndexOfVertex(source)].isVisited = true;

            GraphNode[] adjNodes = graph.GetAdjacentVertex(source);

            foreach (var node in adjNodes)
            {
                if (!node.isVisited)
                {
                    DepthFirstSearch(node.label, graph);
                }
            }
        }
        public static int[] BellmanFordShortPath(string source, GraphDS graph)
        {
            distnace = new int[graph.numberOfVertices];

            for (int i = 0; i < graph.numberOfVertices; i++)
            {
                distnace[i] = Int32.MaxValue;
            }
            distnace[graph.GetIndexOfVertex(source)] = 0;

            for (int i = 0; i < graph.numberOfVertices; i++)
            {
                foreach (var edge in graph.edges)
                {
                    string start       = edge.source;
                    string destination = edge.destination;
                    int    cost        = edge.cost;

                    if (distnace[graph.GetIndexOfVertex(destination)] > distnace[graph.GetIndexOfVertex(start)] + cost)
                    {
                        distnace[graph.GetIndexOfVertex(destination)] = distnace[graph.GetIndexOfVertex(start)] + cost;
                    }
                }
            }
            foreach (var edge in graph.edges)
            {
                string start       = edge.source;
                string destination = edge.destination;
                int    cost        = edge.cost;

                if (distnace[graph.GetIndexOfVertex(destination)] > distnace[graph.GetIndexOfVertex(start)] + cost)
                {
                    Console.WriteLine("Negative cycle found");
                    break;
                }
            }
            return(distnace);
        }
Exemple #5
0
        public static void BreadthFirstSearch(string source, GraphDS graph)
        {
            Queue <GraphNode> q = new Queue <GraphNode>();

            q.Enqueue(graph.vertices[graph.GetIndexOfVertex(source)]);

            while (q.Count > 0)
            {
                GraphNode temp = q.Dequeue();
                temp.isVisited = true;
                Console.WriteLine(temp.label);

                GraphNode[] adjNodes = graph.GetAdjacentVertex(temp.label);

                foreach (var node in adjNodes)
                {
                    if (!node.isVisited)
                    {
                        q.Enqueue(node);
                    }
                }
            }
        }
        public static int[] SSSPBFS(string source, GraphDS graph)
        {
            Queue <GraphNode> q = new Queue <GraphNode>();

            q.Enqueue(graph.vertices[graph.GetIndexOfVertex(source)]);
            int[] distance = new int[graph.vertices.Length];
            distance[graph.GetIndexOfVertex(graph.vertices[graph.GetIndexOfVertex(source)].label)] = 0;
            while (q.Count > 0)
            {
                GraphNode current = q.Dequeue();
                current.isVisited = true;
                GraphNode[] adjNodes = graph.GetAdjacentVertex(current.label);
                foreach (var node in adjNodes)
                {
                    if (!node.isVisited)
                    {
                        q.Enqueue(node);
                        node.isVisited = true;
                        distance[graph.GetIndexOfVertex(node.label)] = distance[graph.GetIndexOfVertex(current.label)] + 1;
                    }
                }
            }
            return(distance);
        }
        static void Main(string[] args)
        {
            //LinkedListDS.InsertNth(3, 0);
            //LinkedListDS.InsertNth(5, 1);
            //LinkedListDS.InsertNth(4, 2);
            //LinkedListDS.InsertNth(2, 3);
            //LLNode head = LinkedListDS.InsertNth(10, 1);
            //head = LinkedListDS.InsertAtLast(6);
            //head = LinkedListDS.DeleteAtBeg();
            //head = LinkedListDS.DeleteAtLast();
            //head = LinkedListDS.InsertAfter(3, 7);
            //head = LinkedListDS.Reverse(head, 3);

            ////LinkedListDS.Lexo();

            ////LinkedListDS.Anagram("xaxbbbxx");
            //string output = LinkedListDS.isBalanced("((()())");
            //int output1 = LinkedListDS.ValidPairs("()()()");

            //TreeDS tree = new TreeDS();

            //tree.root = TreeDS.InsertBST(tree.root, 20);
            //tree.root = TreeDS.InsertBST(tree.root, 8);
            //tree.root = TreeDS.InsertBST(tree.root, 22);
            //tree.root = TreeDS.InsertBST(tree.root, 4);
            //tree.root = TreeDS.InsertBST(tree.root, 12);
            //tree.root = TreeDS.InsertBST(tree.root, 10);
            //tree.root = TreeDS.InsertBST(tree.root, 14);

            //tree.root = new BTNode(20);
            //tree.root.left = new BTNode(8);
            //tree.root.right = new BTNode(22);
            //tree.root.left.left = new BTNode(4);
            //tree.root.left.right = new BTNode(12);
            //tree.root.left.right.left = new BTNode(10);
            //tree.root.left.right.right = new BTNode(14);
            //TreeDS.PreOrder(tree.root);
            //TreeDS.InOrder(tree.root);
            //TreeDS.PostOrder(tree.root);
            //int height = TreeDS.HeightOfBT(tree.root);
            //int count = TreeDS.CountOfBT(tree.root);
            //int max = TreeDS.MaxNode(tree.root);
            //int NumberOfLeaf = TreeDS.NumberOfLeaf(tree.root);
            //BTNode searchFound = TreeDS.SearchNode(tree.root, 2);
            //BTNode LCA = TreeDS.LCA(tree.root, 3, 4);
            //BTNode BSTLCA = TreeDS.BSTLCA(tree.root, 10, 14);
            //bool isBST = TreeDS.PerfectIsBST(tree.root, Int32.MinValue, Int32.MaxValue);

            //int[] result = MergeProblems.MergeArray(new int[] { 4, 5, 6, 7, 8, 9 }, new int[] { 1, 2, 3, 4 });
            //int minNumPlatform = MergeProblems.MinimumNumberOfPlatform(new int[] { 900, 935, 945, 1100, 1430, 1800 }, new int[] { 915, 1145, 1105, 1200, 1815, 1900 });
            //int knapSum = MergeProblems.MaxKnapsack(new int[] { 60, 120, 100 }, new int[] { 10, 30, 20 }, 50);
            //int editDistance = EditDistance.EditDistanceFunction("cat", "brat", 1, 1, 1);
            //int searchIndex = SearchAlgo.BinarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 1);
            //int[] search2D = SearchAlgo.BinarySearch2D(new int[,] { {10, 20, 30, 40},
            //            {15, 25, 35, 45},
            //            {27, 29, 37, 48},
            //            {32, 33, 39, 50} }, 29, 4);
            //int xorResult = SearchAlgo.GetOddNumberFromString("13122344455664115");

            //int[] leftArray = HackerRank.LeftRotate(new int[] { 1, 2, 3, 4, 5 }, 4);

            //MergeList
            //LLNode head1 = LinkedListDS.InsertAtLast(1);
            //head1 = LinkedListDS.InsertAtLast(2);
            //head1 = LinkedListDS.InsertAtLast(4);
            //head1 = LinkedListDS.InsertAtLast(5);

            //LLNode head2 = LinkedListDS.InsertAtLastHead2(3);
            //head2 = LinkedListDS.InsertAtLastHead2(6);
            //head2 = LinkedListDS.InsertAtLastHead2(8);
            //head2 = LinkedListDS.InsertAtLastHead2(9);

            //LLNode mergeList = LinkedListDS.MergeList(head1, head2);

            //int sherlock = HackerRank.SherlockHolmesProblem(1);
            ////int operationResult = HackerRank.OperationArray();
            //HackerRank.Enque(1);
            //HackerRank.Enque(2);
            //HackerRank.Enque(3);
            //HackerRank.Deque();
            //HackerRank.Enque(4);
            //int last = HackerRank.Deque();

            GraphDS graph = new GraphDS(4);

            graph.AddVertex(new GraphNode(false, "A"));
            graph.AddVertex(new GraphNode(false, "B"));
            graph.AddVertex(new GraphNode(false, "C"));
            graph.AddVertex(new GraphNode(false, "D"));
            graph.AddEdgeDirected("A", "B", 5);
            graph.AddEdgeDirected("B", "C", 3);
            graph.AddEdgeDirected("C", "D", 1);
            graph.AddEdgeDirected("A", "D", 10);

            //GraphNode[] adjNodes = graph.GetAdjacentVertex("C");
            //DFS.DepthFirstSearch("A", graph);
            //int[] sssp = SSSP.SSSPBFS("A", graph);
            //int[] bellford = BellmanFord.BellmanFordShortPath("A", graph);
            //int[,] floyd = Floyd.FloydAlgo(graph);

            int[] sortedResult = SortingDS.InsertionSort(new int[] { 3, 8, 1, 5, 10 });

            Console.Read();
        }