Exemplo n.º 1
0
        public void CreateEdges(int num_edges)
        {
            Console.WriteLine("================================================================");

            Console.WriteLine("Creating " + num_edges + " edges");
            const int RANDOM_SEED = 55555;

            DateTime dt   = DateTime.Now;
            Node     from = new Node();
            Node     to   = new Node();

            for (int z = 0; z < (5 * num_edges); z++)
            {
                // Get two random nodes from node_list
                Random rand = new Random(RANDOM_SEED + z);
                int    r    = rand.Next(node_list.Count);
                //Console.WriteLine("First r = " + r);
                from = searchNode(r);
                // get one more random number
                r = rand.Next(node_list.Count);
                //Console.WriteLine("second r = " + r);
                to = searchNode(r);

                // dont allow self loops as of now
                if (from.node_id == to.node_id)
                {
                    r  = rand.Next(node_list.Count);
                    to = searchNode(r);
                }
                // avoid creating duplpicate edges between nodes
                if (isEdge(from.node_id, to.node_id))
                {
                    Console.WriteLine("Avoiding duplicate edges");
                    r  = rand.Next(node_list.Count);
                    to = searchNode(r);
                }

                //now we have 2 random nodes. see if we can hook up these two
                Waxman w       = new Waxman();
                double value   = 0.0;
                double compare = 0.0;
                compare = rand.NextDouble();
                value   = w.ProbFunc(from, to);
                //Console.WriteLine("Compare = " + compare + " value = " + value);

                Random rndm = new Random();
                if (value < compare)
                {
                    int rndm_num = rand.Next(1, 100);

                    Console.WriteLine("Connecting edge from " + to.node_id + " to " + from.node_id + " with cost = " + rndm_num);
                    from.addAdjNode(to, rndm_num);
                    to.addAdjNode(from, rndm_num);
                }
            }
        }
Exemplo n.º 2
0
        public void CreateEdges(int num_edges)
        {
            Console.WriteLine("================================================================");

            Console.WriteLine("Creating " + num_edges + " edges");
            const int RANDOM_SEED = 55555;

            DateTime dt = DateTime.Now;
            Node from = new Node();
            Node to = new Node();
             for (int z = 0; z < (5 * num_edges); z++)
            {
                // Get two random nodes from node_list
                Random rand = new Random(RANDOM_SEED + z);
                int r = rand.Next(node_list.Count);
                //Console.WriteLine("First r = " + r);
                from = searchNode(r);
                // get one more random number
                r = rand.Next(node_list.Count);
                //Console.WriteLine("second r = " + r);
                to = searchNode(r);

                // dont allow self loops as of now
                if (from.node_id == to.node_id)
                {
                    r = rand.Next(node_list.Count);
                    to = searchNode(r);
                }
                // avoid creating duplpicate edges between nodes
                if(isEdge(from.node_id, to.node_id))
                {
                    Console.WriteLine("Avoiding duplicate edges");
                    r = rand.Next(node_list.Count);
                    to = searchNode(r);
                }

                //now we have 2 random nodes. see if we can hook up these two
                Waxman w = new Waxman();
                double value = 0.0;
                double compare = 0.0;
                compare = rand.NextDouble();
                value = w.ProbFunc(from, to);
                //Console.WriteLine("Compare = " + compare + " value = " + value);

                Random rndm = new Random();
                if (value < compare)
                {

                    int rndm_num = rand.Next(1, 100);

                    Console.WriteLine("Connecting edge from " + to.node_id + " to " + from.node_id + " with cost = " + rndm_num);
                    from.addAdjNode(to, rndm_num);
                    to.addAdjNode(from, rndm_num);
                }

            }
        }
Exemplo n.º 3
0
        // for shortest path example
        public void entry1()
        {
            int       num_nodes = 6;
            bool      isGraphConnected;
            GraphImpl gi = new GraphImpl(num_nodes);

            //create 8 nodes


            Node from = new Node();
            Node to   = new Node();

            /***************************** Example 1 ***************************************************
             * //================================= Node 0 ========================================
             * for (int z = 0; z < 8; z++)
             * {
             *  gi.addNode(z, num_nodes);
             * }
             *
             * from = gi.searchNode(0);
             * to = gi.searchNode(1);
             *
             * from.addAdjNode(to, 5);
             * to.addAdjNode(from, 5);
             *
             * to = gi.searchNode(5);
             * from.addAdjNode(to, 3);
             * to.addAdjNode(from, 3);
             *
             * //================================= Node 1 ========================================
             * from = gi.searchNode(1);
             * to = gi.searchNode(2);
             *
             * from.addAdjNode(to, 2);
             * to.addAdjNode(from, 2);
             *
             * to = gi.searchNode(6);
             * from.addAdjNode(to, 3);
             * to.addAdjNode(from, 3);
             *
             * //================================= Node 2 ========================================
             * from = gi.searchNode(2);
             * to = gi.searchNode(3);
             *
             * from.addAdjNode(to, 6);
             * to.addAdjNode(from, 6);
             *
             * to = gi.searchNode(7);
             * from.addAdjNode(to, 10);
             * to.addAdjNode(from, 10);
             * //================================= Node 3 ========================================
             * from = gi.searchNode(3);
             * to = gi.searchNode(4);
             *
             * from.addAdjNode(to, 3);
             * to.addAdjNode(from, 3);
             * //================================= Node 4 ========================================
             * from = gi.searchNode(4);
             * to = gi.searchNode(5);
             *
             * from.addAdjNode(to, 8);
             * to.addAdjNode(from, 8);
             *
             * to = gi.searchNode(7);
             * from.addAdjNode(to, 5);
             * to.addAdjNode(from, 5);
             * //================================= Node 5 ========================================
             * from = gi.searchNode(5);
             * to = gi.searchNode(6);
             *
             * from.addAdjNode(to, 7);
             * to.addAdjNode(from, 7);
             *
             * //================================= Node 6 ========================================
             * from = gi.searchNode(6);
             * to = gi.searchNode(7);
             *
             * from.addAdjNode(to, 2);
             * to.addAdjNode(from, 2);
             * //================================= Node 7 ========================================
             * //all done
             ****************************** Example 1 End ***************************************************/

            //****************************** Example 2 ***************************************************
            for (int z = 0; z < 6; z++)
            {
                gi.addNode(z, num_nodes);
            }

            //================================= Node 0 ========================================
            from = gi.searchNode(0);
            to   = gi.searchNode(1);

            from.addAdjNode(to, 7);
            to.addAdjNode(from, 7);

            to = gi.searchNode(4);

            from.addAdjNode(to, 14);
            to.addAdjNode(from, 14);

            to = gi.searchNode(5);
            from.addAdjNode(to, 9);
            to.addAdjNode(from, 9);

            //================================= Node 1 ========================================
            from = gi.searchNode(1);
            to   = gi.searchNode(2);

            from.addAdjNode(to, 15);
            to.addAdjNode(from, 15);
            to = gi.searchNode(5);

            from.addAdjNode(to, 10);
            to.addAdjNode(from, 10);


            //================================= Node 2 ========================================
            from = gi.searchNode(2);
            to   = gi.searchNode(3);

            from.addAdjNode(to, 6);
            to.addAdjNode(from, 6);
            to = gi.searchNode(5);

            from.addAdjNode(to, 11);
            to.addAdjNode(from, 11);

            //================================= Node 3 ========================================
            from = gi.searchNode(3);
            to   = gi.searchNode(4);

            from.addAdjNode(to, 9);
            to.addAdjNode(from, 9);

            //================================= Node 4 ========================================
            from = gi.searchNode(4);
            to   = gi.searchNode(5);

            from.addAdjNode(to, 2);
            to.addAdjNode(from, 2);

            //================================= Node 5 ========================================


            /****************************** Example 2 End ***************************************************/

            gi.display_connections();
            isGraphConnected = gi.isGraphConnected();
            if (!isGraphConnected)
            {
                Console.WriteLine("Graph not connected");
            }
            else
            {
                Console.WriteLine("Graph connected");

                Dijkstra dij = new Dijkstra();
                // source = 0, dest = 3
                dij.DikkstraImpl(gi, 0, 3);
            }
        }
Exemplo n.º 4
0
        // for shortest path example
        public void entry1()
        {
            int num_nodes = 6;
            bool isGraphConnected;
            GraphImpl gi = new GraphImpl(num_nodes);

            //create 8 nodes

            Node from = new Node();
            Node to = new Node();

            /***************************** Example 1 ***************************************************
            //================================= Node 0 ========================================
            for (int z = 0; z < 8; z++)
            {
                gi.addNode(z, num_nodes);
            }

            from = gi.searchNode(0);
            to = gi.searchNode(1);

            from.addAdjNode(to, 5);
            to.addAdjNode(from, 5);

            to = gi.searchNode(5);
            from.addAdjNode(to, 3);
            to.addAdjNode(from, 3);

            //================================= Node 1 ========================================
            from = gi.searchNode(1);
            to = gi.searchNode(2);

            from.addAdjNode(to, 2);
            to.addAdjNode(from, 2);

            to = gi.searchNode(6);
            from.addAdjNode(to, 3);
            to.addAdjNode(from, 3);

            //================================= Node 2 ========================================
            from = gi.searchNode(2);
            to = gi.searchNode(3);

            from.addAdjNode(to, 6);
            to.addAdjNode(from, 6);

            to = gi.searchNode(7);
            from.addAdjNode(to, 10);
            to.addAdjNode(from, 10);
            //================================= Node 3 ========================================
            from = gi.searchNode(3);
            to = gi.searchNode(4);

            from.addAdjNode(to, 3);
            to.addAdjNode(from, 3);
            //================================= Node 4 ========================================
            from = gi.searchNode(4);
            to = gi.searchNode(5);

            from.addAdjNode(to, 8);
            to.addAdjNode(from, 8);

            to = gi.searchNode(7);
            from.addAdjNode(to, 5);
            to.addAdjNode(from, 5);
            //================================= Node 5 ========================================
            from = gi.searchNode(5);
            to = gi.searchNode(6);

            from.addAdjNode(to, 7);
            to.addAdjNode(from, 7);

            //================================= Node 6 ========================================
            from = gi.searchNode(6);
            to = gi.searchNode(7);

            from.addAdjNode(to, 2);
            to.addAdjNode(from, 2);
            //================================= Node 7 ========================================
            //all done
            ****************************** Example 1 End ***************************************************/

            //****************************** Example 2 ***************************************************
            for (int z = 0; z < 6; z++)
            {
                gi.addNode(z, num_nodes);
            }

            //================================= Node 0 ========================================
            from = gi.searchNode(0);
            to = gi.searchNode(1);

            from.addAdjNode(to, 7);
            to.addAdjNode(from, 7);

            to = gi.searchNode(4);

            from.addAdjNode(to, 14);
            to.addAdjNode(from, 14);

            to = gi.searchNode(5);
            from.addAdjNode(to, 9);
            to.addAdjNode(from, 9);

            //================================= Node 1 ========================================
            from = gi.searchNode(1);
            to = gi.searchNode(2);

            from.addAdjNode(to, 15);
            to.addAdjNode(from, 15);
            to = gi.searchNode(5);

            from.addAdjNode(to, 10);
            to.addAdjNode(from, 10);

            //================================= Node 2 ========================================
            from = gi.searchNode(2);
            to = gi.searchNode(3);

            from.addAdjNode(to, 6);
            to.addAdjNode(from, 6);
            to = gi.searchNode(5);

            from.addAdjNode(to, 11);
            to.addAdjNode(from, 11);

            //================================= Node 3 ========================================
            from = gi.searchNode(3);
            to = gi.searchNode(4);

            from.addAdjNode(to, 9);
            to.addAdjNode(from, 9);

            //================================= Node 4 ========================================
            from = gi.searchNode(4);
            to = gi.searchNode(5);

            from.addAdjNode(to, 2);
            to.addAdjNode(from, 2);

            //================================= Node 5 ========================================

            /****************************** Example 2 End ***************************************************/

            gi.display_connections();
            isGraphConnected = gi.isGraphConnected();
            if (!isGraphConnected)
            {
                Console.WriteLine("Graph not connected");
            }
            else
            {
                Console.WriteLine("Graph connected");

                Dijkstra dij = new Dijkstra();
                // source = 0, dest = 3
                dij.DikkstraImpl(gi, 0, 3);
            }
        }