问题
I was looking at this.
If I were just given a list that contained the number of edges (graph), pair of edges, a origin and a destination, how would I figure out if there is or isn't a path?
I have some idea, but just need a bit of help in terms of starting in scheme.
(is_it_a_path? '(4 ((1 2) (2 3) (3 4) (2 4))) 1 4) ; returns true
(is_it_a_path? '(3 ((1 2) (2 3) (3 1))) 2 3) ; also returns true
In the following 4 is the number of vertices, (1 2)... and so are are the edges and 1 is the start and 4 is the end. Basically from these you are looking at whether there is a path from 1 to 4 in the following defined graph. I hope that can clarify what I mean.
回答1:
Did you see John Clements's earlier response to look at How to Design Programs? It has a Chapter on how to design programs that deal with graphs.
As a meta-answer: the question you're asking, about how to get started on a problem that you're unfamiliar with, is the heart of the HtDP book. Have you looked at it? It's essentially an adaptation of Polya's How To Solve It, but tailored for writing computer programs rather than math proofs. The Second edition draft may be easier to read.
来源:https://stackoverflow.com/questions/9426788/depth-first-search-dfs-for-undirected-graphs