Exemplo n.º 1
0
 public void InitializeGraph(Graph g, bool directed, List<Node> nodesList)
 {
     g.Edges = new LinkedList<Node>[nodesList.Count];
     for (int i = 0; i < nodesList.Count; i++)
     {
         g.Edges[i] = new LinkedList<Node>();
         g.Edges[i].AddFirst(nodesList[i]);
     }
 }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
 public void InsertEdges(Graph g, bool directed, List<Tuple<Node, Node>> edgesToAdd)
 {
     edgesToAdd.ForEach(edge => this.InsertEdge(g, directed, edge.Item1, edge.Item2));
 }