コード例 #1
0
        public Graph_m create_random_graph(int v, int e, bool if_directed) // random graph with v vertices and e edges,can be directed
        {
            Random  r            = new Random();
            Graph_m random_graph = new Graph_m(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_matrix[v1, v2] == false)
                {
                    adj_matrix[v1, v2] = true;
                    if (if_directed == false)
                    {
                        adj_matrix[v2, v1] = true;
                    }
                }
                else
                {
                    i--;
                }
            }
            return(random_graph);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: dennisDP/Graph-in-C-sharp
        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();
        }