Ejemplo n.º 1
0
        /// <summary>
        /// Read a file with a graph which is represented by adjacency list and construct it
        /// </summary>
        /// <param name="graph">graph</param>
        /// <param name="streamReader">streamReader</param>
        private void ReadFileAdjacencyList(Graph.IGraphEdgeListInterface graph, StreamReader streamReader)
        {
            // Variable
            string line;
            bool   isK1 = false; // A vertex with degree 0
            string firstVertex = "", secondVertex;

            // Read graph
            while ((line = streamReader.ReadLine()) != "")
            {
                if (line == null)
                {
                    return;
                }

                if (!line.StartsWith(LEFTSEPARATORADJACENCYLIST) && !line.EndsWith(RIGHTSEPARATORADJACENCYLIST))
                {
                    if (isK1)
                    {
                        graph.AddVertex(firstVertex);
                    }

                    firstVertex = line;
                    isK1        = true;
                    continue;
                }

                if (line.StartsWith(LEFTSEPARATORADJACENCYLIST) && line.EndsWith(RIGHTSEPARATORADJACENCYLIST))
                {
                    secondVertex = line.Substring(1, line.Length - 2);
                    graph.AddEdge(firstVertex, secondVertex);
                    isK1 = false;
                    continue;
                }

                throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Invalid graph");
            }

            // Last vertex doesn't have any edge
            if (isK1)
            {
                graph.AddVertex(firstVertex);
            }

            graph.InitializeGraph();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read a file with a graph which is represented by edge list and construct it
        /// </summary>
        /// <param name="graph">graph</param>
        /// <param name="streamReader">streamReader</param>
        private void ReadFileEdgeList(Graph.IGraphEdgeListInterface graph, StreamReader streamReader)
        {
            // Variable
            string line;

            string[]     edge = null;
            const string LEFTSEPARATOR = "(";
            const string RIGHTSEPARATOR = ")";
            int          startVertex1Index = 0, endVertex1Index = 0;
            int          startVertex2Index = 0, endVertex2Index = 0;

            // Read graph
            while ((line = streamReader.ReadLine()) != "")
            {
                if (line.StartsWith(LEFTSEPARATOR) || line.EndsWith(RIGHTSEPARATOR))
                {
                    edge = new string[2];

                    // (Vertex name) VertexName
                    if (line.StartsWith(LEFTSEPARATOR) && !line.EndsWith(RIGHTSEPARATOR))
                    {
                        startVertex1Index = 1;
                        endVertex1Index   = line.IndexOf(RIGHTSEPARATOR);
                        startVertex2Index = endVertex1Index + 2;
                        endVertex2Index   = line.Length;

                        if (endVertex1Index == -1)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Invalid graph");
                        }
                    }
                    else
                    {
                        // VertexName (Vertex name)
                        if (!line.StartsWith(LEFTSEPARATOR) && line.EndsWith(RIGHTSEPARATOR))
                        {
                            startVertex1Index = 0;
                            endVertex1Index   = line.IndexOf(LEFTSEPARATOR) - 1;
                            startVertex2Index = endVertex1Index + 2;
                            endVertex2Index   = line.Length - 1;

                            if (endVertex1Index == -2)
                            {
                                throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Invalid graph");
                            }
                        }
                        // (Vertex name) (Vertex name)
                        else
                        {
                            startVertex1Index = 1;
                            endVertex1Index   = line.IndexOf(RIGHTSEPARATOR);
                            startVertex2Index = line.LastIndexOf(LEFTSEPARATOR) + 1;
                            endVertex2Index   = line.Length - 1;

                            if (startVertex1Index == startVertex2Index && endVertex1Index == endVertex2Index)
                            {
                                throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Invalid graph");
                            }
                        }
                    }

                    edge[0] = line.Substring(startVertex1Index, endVertex1Index - startVertex1Index);
                    edge[1] = line.Substring(startVertex2Index, endVertex2Index - startVertex2Index);
                }
                else
                {
                    edge = line.Split(SEPARATOR);
                }

                if (edge.Length != 2)
                {
                    throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Invalid graph");
                }

                graph.AddEdge(edge[0], edge[1]);
            }

            graph.FullGenerateVertices();

            graph.InitializeGraph();
        }