Difference between revisions of "99 questions/80 to 89"
(→Graphs: Stole graph descriptions and images from P99 homepage) 
(stubbed in problem descriptions and link to solutions; moved existing solution for P81 to 99 questions/Solutions/81) 

Line 86:  Line 86:  
== Problem 80 == 
== Problem 80 == 

−  <Problem description> 

+  (***) Conversions 

+  
+  Write predicates to convert between the different graph representations. With these predicates, all representations are equivalent; i.e. for the following problems you can always pick freely the most convenient form. The reason this problem is rated (***) is not because it's particularly difficult, but because it's a lot of work to deal with all the special cases. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/80  Solutions ]] 

== Problem 81 == 
== Problem 81 == 

−  Path from one node to another one 
+  (**) Path from one node to another one 
−  +  
−  +  Write a function that, given two nodes a and b in a graph, returns all the acyclic paths from a to b. 

+  
Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

+  
+  <haskell> 

paths 1 4 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)] 
paths 1 4 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)] 

[[1,2,3,4],[1,3,4]] 
[[1,2,3,4],[1,3,4]] 

paths 2 6 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)] 
paths 2 6 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)] 

[] 
[] 

−  </ 
+  </haskell> 
−  Solution: 

+  [[99 questions/Solutions/81  Solutions ]] 

−  <haskell> 

−  import List (elem) 

−  paths :: Eq a => a > a > [(a,a)] > [[a]] 

−  paths a b g = paths1 a b g [] 

−  
−  paths1 :: Eq a => a > a > [(a,a)] > [a] > [[a]] 

−  paths1 a b g current = paths2 a b g current [ y  (x,y) < g, x == a ] 

−  
−  paths2 :: Eq a => a > a > [(a,a)] > [a] > [a] > [[a]] 

−  paths2 a b g current []  a == b = [current++[b]] 

−   otherwise = [] 

−  paths2 a b g current (x:xs)  a == b = [current++[b]] 

−   elem a current = [] 

−   otherwise = (paths1 x b g (current++[a])) ++ (paths2 a b g current xs) 

−  </haskell> 

−  
−  This solution uses a representation of a (directed) graph as a list of arcs (a,b). 

== Problem 82 == 
== Problem 82 == 

−  <Problem description> 

+  (*) Cycle from a given node 

+  
+  Write a predicate cycle(G,A,P) to find a closed path (cycle) P starting at a given node A in the graph G. The predicate should return all cycles via backtracking. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/82  Solutions ]] 

+  
== Problem 83 == 
== Problem 83 == 

−  <Problem description> 

+  (**) Construct all spanning trees 

+  
+  Write a predicate s_tree(Graph,Tree) to construct (by backtracking) all spanning trees of a given graph. With this predicate, find out how many spanning trees there are for the graph depicted to the left. The data of this example graph can be found in the file p83.dat. When you have a correct solution for the s_tree/2 predicate, use it to define two other useful predicates: is_tree(Graph) and is_connected(Graph). Both are fiveminutes tasks! 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/83  Solutions ]] 

+  
== Problem 84 == 
== Problem 84 == 

−  <Problem description> 

+  (**) Construct the minimal spanning tree 

+  
+  Write a predicate ms_tree(Graph,Tree,Sum) to construct the minimal spanning tree of a given labelled graph. Hint: Use the algorithm of Prim. A small modification of the solution of P83 does the trick. The data of the example graph to the right can be found in the file p84.dat. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/84  Solutions ]] 

+  
== Problem 85 == 
== Problem 85 == 

−  <Problem description> 

+  (**) Graph isomorphism 

+  
+  Two graphs G1(N1,E1) and G2(N2,E2) are isomorphic if there is a bijection f: N1 > N2 such that for any nodes X,Y of N1, X and Y are adjacent if and only if f(X) and f(Y) are adjacent. 

+  
+  Write a predicate that determines whether two graphs are isomorphic. Hint: Use an openended list to represent the function f. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/85  Solutions ]] 

+  
== Problem 86 == 
== Problem 86 == 

−  <Problem description> 

+  (**) Node degree and graph coloration 

+  
+  a) Write a predicate degree(Graph,Node,Deg) that determines the degree of a given node. 

+  
+  b) Write a predicate that generates a list of all nodes of a graph sorted according to decreasing degree. 

+  
+  c) Use WelchPowell's algorithm to paint the nodes of a graph in such a way that adjacent nodes have different colors. 

+  
−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/86  Solutions ]] 

+  
== Problem 87 == 
== Problem 87 == 

−  <Problem description> 

+  (**) Depthfirst order graph traversal (alternative solution) 

+  
+  Write a predicate that generates a depthfirst order graph traversal sequence. The starting point should be specified, and the output should be a list of nodes that are reachable from this starting point (in depthfirst order). 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/87  Solutions ]] 

+  
== Problem 88 == 
== Problem 88 == 

−  <Problem description> 

+  (**) Connected components (alternative solution) 

+  
+  Write a predicate that splits a graph into its connected components. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/88  Solutions ]] 

+  
== Problem 89 == 
== Problem 89 == 

−  <Problem description> 

+  (**) Bipartite graphs 

+  
+  Write a predicate that finds out whether a given graph is bipartite. 

−  <pre> 

Example: 
Example: 

+  
+  <pre> 

<example in lisp> 
<example in lisp> 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  < 
+  <example in Haskell> 
</haskell> 
</haskell> 

−  <description of implementation> 

+  [[99 questions/Solutions/89  Solutions ]] 

+  
[[Category:Tutorials]] 
[[Category:Tutorials]] 
Revision as of 01:31, 18 July 2010
This is part of NinetyNine Haskell Problems, based on NinetyNine Prolog Problems.
If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.
Graphs
A graph is defined as a set of nodes and a set of edges, where each edge is a pair of nodes.
There are several ways to represent graphs in Prolog. One method is to represent each edge separately as one clause (fact). In this form, the graph depicted below is represented as the following predicate:
edge(h,g). edge(k,f). edge(f,b). ...
We call this edgeclause form. Obviously, isolated nodes cannot be represented. Another method is to represent the whole graph as one data object. According to the definition of the graph as a pair of two sets (nodes and edges), we may use the following Prolog term to represent the example graph:
graph([b,c,d,f,g,h,k],[e(b,c),e(b,f),e(c,f),e(f,k),e(g,h)])
We call this graphterm form. Note, that the lists are kept sorted, they are really sets, without duplicated elements. Each edge appears only once in the edge list; i.e. an edge from a node x to another node y is represented as e(x,y), the term e(y,x) is not present. The graphterm form is our default representation. In SWIProlog there are predefined predicates to work with sets.
A third representation method is to associate with each node the set of nodes that are adjacent to that node. We call this the adjacencylist form. In our example:
[n(b,[c,f]), n(c,[b,f]), n(d,[]), n(f,[b,c,k]), ...]
The representations we introduced so far are Prolog terms and therefore well suited for automated processing, but their syntax is not very userfriendly. Typing the terms by hand is cumbersome and errorprone. We can define a more compact and "humanfriendly" notation as follows: A graph is represented by a list of atoms and terms of the type XY (i.e. functor '' and arity 2). The atoms stand for isolated nodes, the XY terms describe edges. If an X appears as an endpoint of an edge, it is automatically defined as a node. Our example could be written as:
[bc, fc, gh, d, fb, kf, hg]
We call this the humanfriendly form. As the example shows, the list does not have to be sorted and may even contain the same edge multiple times. Notice the isolated node d. (Actually, isolated nodes do not even have to be atoms in the Prolog sense, they can be compound terms, as in d(3.75,blue) instead of d in the example).
When the edges are directed we call them arcs. These are represented by ordered pairs. Such a graph is called directed graph. To represent a directed graph, the forms discussed above are slightly modified. The example graph above is represented as follows:
Arcclause form
arc(s,u). arc(u,r). ...
Graphterm form
digraph([r,s,t,u,v],[a(s,r),a(s,u),a(u,r),a(u,s),a(v,u)])
Adjacencylist form
[n(r,[]),n(s,[r,u]),n(t,[]),n(u,[r]),n(v,[u])]
Note that the adjacencylist does not have the information on whether it is a graph or a digraph.
Humanfriendly form
[s > r, t, u > r, s > u, u > s, v > u]
Finally, graphs and digraphs may have additional information attached to nodes and edges (arcs). For the nodes, this is no problem, as we can easily replace the single character identifiers with arbitrary compound terms, such as city('London',4711). On the other hand, for edges we have to extend our notation. Graphs with additional information attached to edges are called labelled graphs.
Arcclause form
arc(m,q,7). arc(p,q,9). arc(p,m,5).
Graphterm form
digraph([k,m,p,q],[a(m,p,7),a(p,m,5),a(p,q,9)])
Adjacencylist form
[n(k,[]),n(m,[q/7]),n(p,[m/5,q/9]),n(q,[])]
Notice how the edge information has been packed into a term with functor '/' and arity 2, together with the corresponding node.
Humanfriendly form
[p>q/9, m>q/7, k, p>m/5]
The notation for labelled graphs can also be used for socalled multigraphs, where more than one edge (or arc) are allowed between two given nodes.
Problem 80
(***) Conversions
Write predicates to convert between the different graph representations. With these predicates, all representations are equivalent; i.e. for the following problems you can always pick freely the most convenient form. The reason this problem is rated (***) is not because it's particularly difficult, but because it's a lot of work to deal with all the special cases.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 81
(**) Path from one node to another one
Write a function that, given two nodes a and b in a graph, returns all the acyclic paths from a to b.
Example:
<example in lisp>
Example in Haskell:
paths 1 4 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)]
[[1,2,3,4],[1,3,4]]
paths 2 6 [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)]
[]
Problem 82
(*) Cycle from a given node
Write a predicate cycle(G,A,P) to find a closed path (cycle) P starting at a given node A in the graph G. The predicate should return all cycles via backtracking.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 83
(**) Construct all spanning trees
Write a predicate s_tree(Graph,Tree) to construct (by backtracking) all spanning trees of a given graph. With this predicate, find out how many spanning trees there are for the graph depicted to the left. The data of this example graph can be found in the file p83.dat. When you have a correct solution for the s_tree/2 predicate, use it to define two other useful predicates: is_tree(Graph) and is_connected(Graph). Both are fiveminutes tasks!
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 84
(**) Construct the minimal spanning tree
Write a predicate ms_tree(Graph,Tree,Sum) to construct the minimal spanning tree of a given labelled graph. Hint: Use the algorithm of Prim. A small modification of the solution of P83 does the trick. The data of the example graph to the right can be found in the file p84.dat.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 85
(**) Graph isomorphism
Two graphs G1(N1,E1) and G2(N2,E2) are isomorphic if there is a bijection f: N1 > N2 such that for any nodes X,Y of N1, X and Y are adjacent if and only if f(X) and f(Y) are adjacent.
Write a predicate that determines whether two graphs are isomorphic. Hint: Use an openended list to represent the function f.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 86
(**) Node degree and graph coloration
a) Write a predicate degree(Graph,Node,Deg) that determines the degree of a given node.
b) Write a predicate that generates a list of all nodes of a graph sorted according to decreasing degree.
c) Use WelchPowell's algorithm to paint the nodes of a graph in such a way that adjacent nodes have different colors.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 87
(**) Depthfirst order graph traversal (alternative solution)
Write a predicate that generates a depthfirst order graph traversal sequence. The starting point should be specified, and the output should be a list of nodes that are reachable from this starting point (in depthfirst order).
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 88
(**) Connected components (alternative solution)
Write a predicate that splits a graph into its connected components.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>
Problem 89
(**) Bipartite graphs
Write a predicate that finds out whether a given graph is bipartite.
Example:
<example in lisp>
Example in Haskell:
<example in Haskell>