Esempio n. 1
0
        public AdjGraphNode <K> BreadthFirstSearch(AdjGraphNode <K> startNode, K searchNode, Guid visitId)
        {
            Queue <AdjGraphNode <K> > q = new Queue <AdjGraphNode <K> >();

            startNode.visitId = visitId;
            q.Enqueue(startNode);

            while (q.Count > 0)
            {
                AdjGraphNode <K> n = q.Dequeue();
                Console.WriteLine(n.data.ToString());

                if (n.data.Equals(searchNode))
                {
                    return(n);
                }

                foreach (AdjGraphNode <K> adj in n.neighbours)
                {
                    if (adj.visitId != visitId)
                    {
                        adj.visitId = visitId;
                        q.Enqueue(adj);
                    }
                }
            }

            return(null);
        }
Esempio n. 2
0
        private List <K> BreadthFirstTraversal(Guid visitId)
        {
            List <K> BFList = new List <K>();

            Queue <AdjGraphNode <K> > q = new Queue <AdjGraphNode <K> >();

            startNode.visitId = visitId;
            q.Enqueue(startNode);

            while (q.Count > 0)
            {
                AdjGraphNode <K> n = q.Dequeue();

                BFList.Add(n.data);

                foreach (AdjGraphNode <K> adj in n.neighbours)
                {
                    if (adj.visitId != visitId)
                    {
                        adj.visitId = visitId;
                        q.Enqueue(adj);
                    }
                }
            }

            return(BFList);
        }
Esempio n. 3
0
            public override bool Equals(Object obj)
            {
                // Check for null values and compare run-time types.
                if (obj == null || GetType() != obj.GetType())
                {
                    return(false);
                }

                AdjGraphNode <T> node2 = (AdjGraphNode <T>)obj;

                return(Comparer <T> .Default.Compare(this.data, node2.data) == 0);
            }
Esempio n. 4
0
        /// <summary>
        /// Finds if there is a path between A and B using DFS
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public bool PathBetweenABExist(K a, K b, Guid visitId)
        {
            AdjGraphNode <K> nodeA = BreadthFirstSearch(this.startNode, a, Guid.NewGuid());

            if (nodeA != null)
            {
                AdjGraphNode <K> nodeB = BreadthFirstSearch(nodeA, b, Guid.NewGuid());

                if (nodeB != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
        public List <K> DepthFirstTraversal(AdjGraphNode <K> node, Guid visitId, List <K> list)
        {
            if (node.visitId == visitId)
            {
                return(list);
            }

            node.visitId = visitId;
            list.Add(node.data);

            foreach (AdjGraphNode <K> n in node.neighbours)
            {
                list = DepthFirstTraversal(n, visitId, list);
            }

            return(list);
        }
Esempio n. 6
0
        public void AppendNodes(K nodeA, K nodeB, Guid visitId)
        {
            AdjGraphNode <K> a = BreadthFirstSearch(nodeA, Guid.NewGuid());

            if (a != null)
            {
                AdjGraphNode <K> b = BreadthFirstSearch(nodeB, Guid.NewGuid());
                if (b != null)
                {
                    a.neighbours.Add(b);
                }
                else
                {
                    b = new AdjGraphNode <K>(nodeB);
                    a.neighbours.Add(b);
                    count++;
                }
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            AdjGraphNode <string> node1 = new AdjGraphNode <string>("VP and Ellesemere");
            AdjGraphNode <string> node2 = new AdjGraphNode <string>("Markham and Ellesemere");
            AdjGraphNode <string> node3 = new AdjGraphNode <string>("Markam and Eglinton");
            AdjGraphNode <string> node4 = new AdjGraphNode <string>("VP and Eglington");
            AdjGraphNode <string> node5 = new AdjGraphNode <string>("VP and Kingston Rd");
            AdjGraphNode <string> node6 = new AdjGraphNode <string>("Kingston Rd and Eglinton");

            node1.neighbours.Add(node4);
            node1.neighbours.Add(node2);
            node2.neighbours.Add(node3);
            node4.neighbours.Add(node3);
            node4.neighbours.Add(node5);
            node5.neighbours.Add(node6);
            node6.neighbours.Add(node3);

            //node1.DepthFirstTraversal();
            node1.BreadthFirstTraversal();


            //342, 206, 444, 523, 607, 301, 142, 183, 102, 157, 149
            BinaryTree <int> tree = new BinaryTree <int>(342);

            tree.Insert(206);
            tree.Insert(444);
            tree.Insert(523);
            tree.Insert(607);
            tree.Insert(301);
            tree.Insert(142);
            tree.Insert(183);
            tree.Insert(102);
            tree.Insert(157);
            tree.Insert(149);


            tree.BreathFirstTraversal();
        }
Esempio n. 8
0
 public AdjGraph(K data)
 {
     startNode = new AdjGraphNode <K>(data);
     count++;
 }