Exemplo n.º 1
0
    { // using static class as utility/helper. static class can not be instantiated or inherited, to access member, simply use TreeTraversalAlgorithm.BFS() etc
        public static List <int> BFS(graphnode g)
        {
            List <int> res = new List <int>();

            if (g == null)
            {
                return(res);
            }

            Queue <graphnode> q = new Queue <graphnode>();

            res.Add(g.value);
            g.visited = true;
            q.Enqueue(g);
            while (q.Count != 0)
            {
                graphnode cur = q.Dequeue();
                res.Add(cur.value);
                cur.visited = true;
                foreach (graphnode n in cur.children)
                {
                    if (!n.visited)
                    {
                        q.Enqueue(n);
                    }
                }
            }
            return(res);
        }
Exemplo n.º 2
0
 public void addIndirectedEdge(graphnode node1, graphnode node2)
 {
     this.graphnodelist.Where(n => n == node1).FirstOrDefault().children.Add(node2);
     this.graphnodelist.Where(n => n == node2).FirstOrDefault().children.Add(node1);
 }
Exemplo n.º 3
0
        }                                               // adjacent list

        public void addDirectedEdge(graphnode node1, graphnode node2)
        {
            node1.children.Add(node2);
        }