Example #1
0
        public Graph_l create_random_graph(int v, int e, bool if_directed)
        {
            Random  r            = new Random();
            Graph_l random_graph = new Graph_l(v, if_directed);

            random_graph.vertices = v;
            random_graph.edges    = e;
            random_graph.directed = if_directed;

            for (int i = 0; i < e; i++)
            {
                int v1 = r.Next(0, v);
                int v2 = r.Next(0, v);
                if (adj_list[v1].Contains(v2) == false)
                {
                    adj_list[v1].Add(v2);
                    if (if_directed == false)
                    {
                        adj_list[v2].Add(v1);
                    }
                }
                else
                {
                    i--;
                }
            }
            return(random_graph);
        }
Example #2
0
        static void Main(string[] args)
        {
            int start, stop;

            Graph_l g = new Graph_l(1000, false);

            g.insert_edge(0, 4);
            g.insert_edge(4, 3);
            g.insert_edge(3, 5);
            g.insert_edge(3, 6);
            g.insert_edge(4, 2);
            g.insert_edge(4, 1);

            Graph_m g1 = new Graph_m(1000, false);

            g1.insert_edge(0, 4);
            g1.insert_edge(4, 3);
            g1.insert_edge(3, 5);
            g1.insert_edge(3, 6);
            g1.insert_edge(4, 2);
            g1.insert_edge(4, 1);


            int[] path = g1.shortest_path(g1.find_vertex_by_label("label0"), g1.find_vertex_by_label("label6"));

            start = Environment.TickCount;
            for (int i = 0; i < 100000; i++)
            {
                g.shortest_path(0, 6);
            }
            stop = Environment.TickCount;
            Console.WriteLine("Czas pracy dla reprezentacji listowej: " + (stop - start) + "\n");

            start = Environment.TickCount;
            for (int i = 0; i < 100000; i++)
            {
                g1.shortest_path(0, 6);
            }
            stop = Environment.TickCount;
            Console.WriteLine("Czas pracy dla reprezentacji macierzowej: " + (stop - start));

            Console.WriteLine("Najkrótsza ścieżka z wierzckołka 0 do wierzchołka 6: ");
            foreach (int n in path)
            {
                Console.Write(n);
                if (n != 0)
                {
                    Console.Write("<-");
                }
            }
            Console.Read();
        }