Example #1
0
        private void ReadGraph(string filePath)
        {
            //Verify file exists in the path

            if (File.Exists(filePath))
            {
                //read file contents line by line

                using (StreamReader sr = new StreamReader(filePath))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        //split the line on tab to get the two nodes
                        string[] contents = line.Split('\t');

                        int key = Int32.Parse(contents[0]);
                        int Value = Int32.Parse(contents[1]);
                        GraphNode node = new GraphNode(Value);

                        LinkedList<GraphNode> adjList = null; ;
                        if (GraphObject.ContainsKey(key))
                        {

                            adjList = GraphObject[key];
                            if (adjList == null)
                            {
                                adjList = new LinkedList<GraphNode>();
                            }
                            adjList.AddLast(node);
                            //UPDATE THE VALUE
                            GraphObject[key] = adjList;
                        }
                        else
                        {
                            adjList = new LinkedList<GraphNode>();

                            //Add the key node to the beginning
                            adjList.AddFirst(new GraphNode(key));

                            //Add the value node
                            adjList.AddLast(node);

                            GraphObject.Add(key, adjList);
                        }

                        this.Edges++;
                    }
                }

                //Set number of Vertices
                this.Vertices = GraphObject.Count();
            }
        }
Example #2
0
        private void DFSTraversal(GraphNode node, IDictionary<int, bool> Visited)
        {
            int key = node.Key;
            if(Visited[key])
            {
                return;
            }

            // Mark as visited
            Visited[key] = true;
            Console.WriteLine("Visited Node value is {0}", node.Key);

            //Visit its neighbors
            LinkedList<GraphNode> neighbors = GraphObject[key];
            foreach(GraphNode neighbor in neighbors)
            {
                DFSTraversal(neighbor, Visited);
            }
        }
        private void BFS(GraphNode node, IDictionary<int,bool> visited)
        {
            if(node == null)
            {
                return;
            }
            int key = node.Key;
            Queue<GraphNode> queue = new Queue<GraphNode>();
            queue.Enqueue(node);

            while(queue.Count>0)
            {
                GraphNode poppedNode = queue.Dequeue();

                Visited[poppedNode.Key] = true;

                //print visited node
                Console.WriteLine("Visited Node value is {0}", poppedNode.Key);

                LinkedList<GraphNode> neighbors = GraphObject[poppedNode.Key];
                foreach(GraphNode neighbor in neighbors)
                {
                    if(!Visited[neighbor.Key])
                    {
                        queue.Enqueue(neighbor);
                    }
                }

            }
        }
        private void AddEdge(int key, int Value)
        {
            GraphNode node = new GraphNode(Value);

            LinkedList<GraphNode> adjList;
            if (GraphObject.ContainsKey(key))
            {

                adjList = GraphObject[key];
                if (adjList == null)
                {
                    adjList = new LinkedList<GraphNode>();
                }

                //Add to the end
                adjList.AddLast(node);

                //UPDATE THE VALUE
                GraphObject[key] = adjList;
            }
            else
            {
                adjList = new LinkedList<GraphNode>();

                //Add the key node to the beginning
                //adjList.AddFirst(new GraphNode(key));

                //Add the value node
                adjList.AddLast(node);

                GraphObject.Add(key, adjList);
            }
        }
 public int GetHashCode(GraphNode <V> obj)
 {
     return(obj.Value.GetHashCode());
 }
 public bool Equals(GraphNode <V> x, GraphNode <V> y)
 {
     return(x.Index == y.Index && x.Value.Equals(y.Value));
 }
Example #7
0
        /// <summary>
        /// Generate a maze
        /// </summary>
        /// <param name="lenght">lenght</param>
        /// <param name="heigh">heigh</param>
        /// <param name="loops">number of loops in the maze , by default == 0</param>
        /// <returns>a matrix representing the maze</returns>
        public GraphNode<MazeNode, int>[,] GetMaze(int lenght, int heigh, int loops = 0)
        {
            // generate result matrix
            GraphNode<MazeNode, int>[,] result =  new GraphNode<MazeNode, int>[heigh,lenght];

            for(int i = 0;  i < lenght; i++)
            {
                for(int j=0;j<heigh;j++)
                {
                    result[j,i] = new NonDirectedGraph<MazeNode,int>()
                    { Value = new MazeNode()
                    { X =i, Y = j, Type = CellType.empty}};
                }
            }

            // randomly connect it
            Random rnd = new Random();
            // connect lines
            for(int i=0; i<lenght -1 ;i++)
            {
                for(int j=0; j<heigh;j++)
                {
                    result[j,i].AddNeightbour(result[j,i+1],rnd.Next());
                }
            }
            // connect columns
            for(int i=0; i<lenght;i++)
            {
                for(int j=0; j<heigh -1;j++)
                {
                    result[j,i].AddNeightbour(result[j+1,i],rnd.Next(1,Int16.MaxValue));
                }
            }

            // Generate maze by selecting minimum arches
            HashSet<GraphNode<MazeNode,int>> visited = new HashSet<GraphNode<MazeNode,int>>();

            var arches = result.
                Cast<GraphNode<MazeNode, int>>().
                SelectMany(n=>n.Neighbours.Select(l=> new {Start =n, End =l.Key, Lenght = l.Value})).
                OrderByDescending(a=>a.Lenght).
                ToList();

            var first = arches.First();
            visited.Add(first.Start);
            visited.Add(first.End);
            first.Start.Neighbours[first.End] = 0; // connection marked for keeping
            first.End.Neighbours[first.Start] = 0; // connection marked for keeping

            while(visited.Count < lenght * heigh  && arches.Any())
            {
                foreach (var arch in arches)
                {
                    if ((!visited.Contains(arch.Start)) ^ (!visited.Contains(arch.End)))
                    {
                        visited.Add(arch.Start);
                        visited.Add(arch.End);
                        arch.Start.Neighbours[arch.End] = 0; // connection marked for keeping
                        arch.End.Neighbours[arch.Start] = 0; // connection marked for keeping
                        arches.Remove(arch);
                        break;
                    }
                }
            }

            // add loops
            while (loops > 0 && arches.Any())
            {
                var arch = arches.ElementAt(0);
                arches.RemoveAt(0);
                if (arch.Start.Neighbours[arch.End] != 0)
                {
                    // connection marked for keeping
                    arch.Start.Neighbours[arch.End] = 0;
                    arch.End.Neighbours[arch.Start] = 0;
                    loops--;
                }
            }

            foreach (var cell in result)
            {
                foreach (var kvp in cell.Neighbours.ToList())
                {
                    // removed neighbours with arches >0
                    if (kvp.Value > 0)
                    {
                        cell.RemoveNeightbour(kvp.Key);
                    }
                }
            }

            // add start,end

            var start = visited.ElementAt(rnd.Next(visited.Count));
            start.Value.Type = CellType.start;
            visited.Remove(start);

            var end = visited.ElementAt(rnd.Next(visited.Count));
            end.Value.Type = CellType.end;
            visited.Remove(end);

            return result;
        }