Пример #1
0
        // default constructor
        public GraphPrim(string graphFile)
        {
            int      u, v;
            int      e, wgt;
            NodePrim t;

            StreamReader reader = new StreamReader(graphFile);

            char[] splits = new char[] { ' ', ',', '\t' };
            string line   = reader.ReadLine();

            string[] parts = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);

            //find out the number of vertices and edges
            V = int.Parse(parts[0]);
            E = int.Parse(parts[1]);

            //create the sentinel node
            z      = new NodePrim();
            z.next = z;

            //Create adjacency lists, initialised to sentinel node z
            //Dynamically allocate array
            adj = new NodePrim[V + 1];

            for (v = 1; v <= V; ++v)
            {
                adj[v] = z;
            }

            Console.WriteLine("Reading edges from text file");

            for (e = 1; e <= E; ++e)
            {
                line  = reader.ReadLine();
                parts = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);

                u   = int.Parse(parts[0]);
                v   = int.Parse(parts[1]);
                wgt = int.Parse(parts[2]);

                Console.WriteLine("Edge {0}--({1})--{2}", u, wgt, v);

                //code to put edge into adjacency lists
                t      = new NodePrim();
                t.data = wgt;
                t.vert = u;
                t.next = adj[v];

                adj[v] = t;

                t      = new NodePrim();
                t.data = wgt;
                t.vert = v;
                t.next = adj[u];

                adj[u] = t;
            }
            reader.Dispose();
        }
Пример #2
0
        // default constructor
        public GraphPrim(string graphFile)
        {
            int u, v;
            int e, wgt;
            NodePrim t;

            StreamReader reader = new StreamReader(graphFile);

            char[] splits = new char[] { ' ', ',', '\t' };
            string line = reader.ReadLine();
            string[] parts = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);

            //find out the number of vertices and edges
            V = int.Parse(parts[0]);
            E = int.Parse(parts[1]);

            //create the sentinel node
            z = new NodePrim();
            z.next = z;

            //Create adjacency lists, initialised to sentinel node z
            //Dynamically allocate array
            adj = new NodePrim[V + 1];

            for (v = 1; v <= V; ++v)
            {
                adj[v] = z;
            }

            Console.WriteLine("Reading edges from text file");

            for (e = 1; e <= E; ++e)
            {
                line = reader.ReadLine();
                parts = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);

                u = int.Parse(parts[0]);
                v = int.Parse(parts[1]);
                wgt = int.Parse(parts[2]);

                Console.WriteLine("Edge {0}--({1})--{2}", u, wgt, v);

                //code to put edge into adjacency lists
                t = new NodePrim();
                t.data = wgt;
                t.vert = u;
                t.next = adj[v];

                adj[v] = t;

                t = new NodePrim();
                t.data = wgt;
                t.vert = v;
                t.next = adj[u];

                adj[u] = t;
            }
        }