コード例 #1
0
ファイル: Network.cs プロジェクト: MatthewBald/Graph
        public void DFS(Vertex v)
        {
            v.SetLabel("VISITED");

            Console.WriteLine("");
            Console.WriteLine(v.GetId());
            Console.WriteLine(v.GetLabel());

            foreach (Edge e in IncidentEdges(v))
            {
                if (e.GetLabel() == "UNEXPLORED")
                {
                    Vertex w = Opposite(v, e);
                    if (w != null && w.GetLabel() == "UNEXPLORED")
                    {
                        e.SetLabel("DISCOVERY");
                        DFS(w);
                    }
                    else
                    {
                        e.SetLabel("BACK");
                    }
                }
            }
        }
コード例 #2
0
ファイル: Network.cs プロジェクト: MatthewBald/Graph
        public bool AreAdjacent(Vertex v, Vertex w)
        {
            if (v == null || w == null)
            {
                return(false);
            }

            foreach (string id in v.GetConnections())
            {
                if (id == w.GetId())
                {
                    return(true);
                }
            }

            foreach (string id in w.GetConnections())
            {
                if (id == v.GetId())
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
ファイル: Network.cs プロジェクト: MatthewBald/Graph
        /// <summary>
        /// Adds a directed edge from Vertex v to Vertex w
        /// </summary>
        /// <param name="v"></param>
        /// <param name="w"></param>
        public void AddEdge(Vertex v, Vertex w)
        {
            if (v == null || w == null)
            {
                return;
            }

            _edges.Add(new Edge(v, w));
            _vertices.Find(vert => vert.GetId() == v.GetId()).AddConnection(w.GetId());
        }
コード例 #4
0
ファイル: Network.cs プロジェクト: MatthewBald/Graph
        /// <summary>
        /// Returns the opposite Vertex of Vertex v on Edge e
        /// </summary>
        /// <param name="v"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public Vertex Opposite(Vertex v, Edge e)
        {
            if (v == null || e == null)
            {
                return(null);
            }

            if (e.GetVertexA().GetId() == v.GetId())
            {
                return(GetVertex(e.GetVertexB().GetId()));
            }

            else if (e.GetVertexB().GetId() == v.GetId())
            {
                return(GetVertex(e.GetVertexA().GetId()));
            }

            return(null);
        }
コード例 #5
0
ファイル: Network.cs プロジェクト: MatthewBald/Graph
        /// <summary>
        /// Returns a List containing the Edges starting at Vertex v
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public List <Edge> IncidentEdges(Vertex v)
        {
            List <Edge> incidents = new List <Edge>();

            if (v.GetConnections() == null || v.GetConnections().Count == 0)
            {
                return(incidents);
            }

            incidents.AddRange(_edges.FindAll(x => x.GetVertexA().GetId() == v.GetId()));

            return(incidents);
        }
コード例 #6
0
        public GraphBuilder(string filePath)
        {
            _network = new Network();
            try
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    while (!sr.EndOfStream)
                    {
                        string   line    = sr.ReadLine();
                        string[] lineArr = line.Split(',');

                        Vertex a = new Vertex(lineArr[0].Trim());
                        Vertex b = new Vertex(lineArr[1].Trim());

                        if (_network.GetVertex(a.GetId()) == null)
                        {
                            _network.AddVertex(a);
                        }
                        if (_network.GetVertex(b.GetId()) == null)
                        {
                            _network.AddVertex(b);
                        }
                        if (_network.GetEdge(new Edge(a, b)) == null)
                        {
                            _network.AddEdge(a, b);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            //_network.PrintVertices();
            int cComps = _network.BFS();

            Console.WriteLine(cComps);
        }