Example #1
0
File: graph.cs Project: baio/algos
        //4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

        bool findPath(GraphNode start, GraphNode end) {

            var list = new List<GraphNode>();
            list.Add(start);

            GraphNode iter;

            while (list.Count != 0)
            {
                iter = list.First();                
                list.Remove(iter);
                iter.state = State.Visiting;

                foreach (var adj in iter.adj)
                {
                    if (adj.state == State.Unvisited)
                    {
                        if (adj == end)
                        {
                            return true;
                        }
                        else {
                            adj.state = State.Visiting;
                            list.Add(adj);
                        }
                    }

                    iter.state = State.Visited;
                }
            }

            return false;
        }
Example #2
0
File: graph.cs Project: baio/algos
 Graph(GraphNode[] nodes) {
     this.nodes = nodes;
 }
Example #3
0
File: graph.cs Project: baio/algos
 GraphNode(GraphNode[] adj) {
     this.adj = adj;
 }