//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; }
Graph(GraphNode[] nodes) { this.nodes = nodes; }
GraphNode(GraphNode[] adj) { this.adj = adj; }