Esempio n. 1
0
        public void InsertEdge(Graph g, bool directed, Node x, Node y)
        {
            var nodeList = g.Edges.Single(edge => edge.First.Value.Equals(x));
            nodeList.AddAfter(nodeList.First, y);

            if (!directed)
            {
                this.InsertEdge(g, true, y, x);
            }
        }
Esempio n. 2
0
        public void DFS(Graph g, Node start, List<LinkedListNode<Node>> discovered, List<LinkedListNode<Node>> processed)
        {
            var startNode = g.Edges.FirstOrDefault(lNode => lNode.First.Value.Equals(start)).First;
            discovered.Add(startNode);
            this.ProcessNodeEarly(startNode.Value);
            var nextNode = startNode.Next;
            while (nextNode != null)
            {
                this.ProcessEdge(startNode.Value, nextNode.Value);
                var nextNode2 = g.Edges.FirstOrDefault(lNode => lNode.First.Value.Equals(nextNode.Value)).First;
                if (!discovered.Contains(nextNode2))
                {
                    discovered.Add(nextNode2);
                    this.DFS(g, nextNode2.Value, discovered, processed);
                }

                nextNode = nextNode.Next;
            }

            processed.Add(startNode);
        }
Esempio n. 3
0
        public void BFS(Graph g, Node start)
        {
            List<LinkedListNode<Node>> discovered = new List<LinkedListNode<Node>>();
            List<LinkedListNode<Node>> processed = new List<LinkedListNode<Node>>();
            Dictionary<LinkedListNode<Node>, LinkedListNode<Node>> nodeToParent = new Dictionary<LinkedListNode<Node>, LinkedListNode<Node>>();

            var startNode = g.Edges.FirstOrDefault(lNode => lNode.First.Value.Equals(start)).First;
            var q = new Queue<LinkedListNode<Node>>();
            q.Enqueue(startNode);
            discovered.Add(startNode);
            nodeToParent[startNode] = null;

            while (q.Any())
            {
                var currentNode = q.Dequeue();
                this.ProcessNodeEarly(currentNode.Value);
                processed.Add(currentNode);
                var nextNode = currentNode.Next;
                while (nextNode != null)
                {
                    this.ProcessEdge(currentNode.Value, nextNode.Value);
                    var nextNode2 = g.Edges.FirstOrDefault(lNode => lNode.First.Value.Equals(nextNode.Value)).First;
                    if (!discovered.Contains(nextNode2))
                    {
                        q.Enqueue(nextNode2);
                        discovered.Add(nextNode2);
                        nodeToParent[nextNode2] = currentNode;
                    }

                    nextNode = nextNode.Next;
                }
            }

            Console.WriteLine("Node to parent: ");
            foreach (var kvp in nodeToParent)
            {
                var val = kvp.Value != null ? kvp.Value.Value.Y.ToString() : string.Empty;
                Console.WriteLine(kvp.Key.Value.Y + " from " + val);
            }
        }
Esempio n. 4
0
 private void ProcessNodeEarly(Node current)
 {
     Console.WriteLine("Vertex: " + current.Y);
 }
Esempio n. 5
0
 private void ProcessEdge(Node start, Node end)
 {
     Console.WriteLine("Edge: " + start.Y + " to " + end.Y);
 }