예제 #1
0
        private void LoadSimpleFormatGraph(string graphFile)
        {
            FileStream   ifs  = new FileStream(graphFile, FileMode.Open);
            StreamReader sr   = new StreamReader(ifs);
            string       line = "";

            string[] tokens    = null;
            string[] subTokens = null;

            // count number of lines == number of nodes
            int numNodes = 0;
            int numEdges = 0;

            while ((sr.ReadLine() != null))
            {
                ++numNodes;
            }
            sr.Close(); ifs.Close();

            this.data = new LowerTriangularBitMatrix(numNodes);

            // second pass, parse and store
            int fromNode = -1;
            int toNode   = -1;

            ifs = new FileStream(graphFile, FileMode.Open); // reopen file
            sr  = new StreamReader(ifs);
            while ((line = sr.ReadLine()) != null)
            {
                line      = line.Trim();
                tokens    = line.Split(':'); // separate from-node and to-nodes
                tokens[0] = tokens[0].Trim();
                tokens[1] = tokens[1].Trim();
                fromNode  = int.Parse(tokens[0]);
                subTokens = tokens[1].Split(' '); // get the to-nodes
                for (int i = 0; i < subTokens.Length; ++i)
                {
                    toNode = int.Parse(subTokens[i]);
                    if (fromNode < toNode)
                    {
                        // because storage is a lower tiangular matrix
                        this.data.SetValue(fromNode, toNode, true);
                        ++numEdges;
                    }
                }
            }
            sr.Close(); ifs.Close();

            // third pass, get number-adjacent
            // iterate through the LowerTriangularBitMatrix data
            // by using GetData, which works on a virtual n x n matrix
            // we can avoid dealing with the physical representation

            this.numberNeighbors = new int[numNodes];
            for (int row = 0; row < numNodes; ++row)
            {
                int count = 0;
                for (int col = 0; col < numNodes; ++col)
                {
                    if (data.GetValue(row, col) == true)
                    {
                        ++count;
                    }
                }
                numberNeighbors[row] = count;
            }

            this.numberNodes = numNodes;
            this.numberEdges = numEdges;
            return;
        } // LoadSimpleFormatGraph
        private void LoadSimpleFormatGraph(string graphFile)
        {
            FileStream ifs = new FileStream(graphFile, FileMode.Open);
            StreamReader sr = new StreamReader(ifs);
            string line = "";
            string[] tokens = null;
            string[] subTokens = null;

            // count number of lines == number of nodes
            int numNodes = 0;
            int numEdges = 0;
            while ((sr.ReadLine() != null))
            {
                ++numNodes;
            }
            sr.Close(); ifs.Close();

            this.data = new LowerTriangularBitMatrix(numNodes);

            // second pass, parse and store
            int fromNode = -1;
            int toNode = -1;
            ifs = new FileStream(graphFile, FileMode.Open); // reopen file
            sr = new StreamReader(ifs);
            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                tokens = line.Split(':'); // separate from-node and to-nodes
                tokens[0] = tokens[0].Trim();
                tokens[1] = tokens[1].Trim();
                fromNode = int.Parse(tokens[0]);
                subTokens = tokens[1].Split(' '); // get the to-nodes
                for (int i = 0; i < subTokens.Length; ++i)
                {
                    toNode = int.Parse(subTokens[i]);
                    if (fromNode < toNode)
                    {
                        // because storage is a lower tiangular matrix
                        this.data.SetValue(fromNode, toNode, true);
                        ++numEdges;
                    }
                }
            }
            sr.Close(); ifs.Close();

            // third pass, get number-adjacent
            // iterate through the LowerTriangularBitMatrix data
            // by using GetData, which works on a virtual n x n matrix
            // we can avoid dealing with the physical representation

            this.numberNeighbors = new int[numNodes];
            for (int row = 0; row < numNodes; ++row)
            {
                int count = 0;
                for (int col = 0; col < numNodes; ++col)
                {
                    if (data.GetValue(row, col) == true)
                        ++count;
                }
                numberNeighbors[row] = count;
            }

            this.numberNodes = numNodes;
            this.numberEdges = numEdges;
            return;
        } // LoadSimpleFormatGraph