# Exercice sur les graphes en prolog

**URL:** <https://swi-prolog.discourse.group/t/exercice-sur-les-graphes-en-prolog/1654>\
**Category:** Help!\
**Created:** [December 23, 2019, 12:09pm UTC](https://swi-prolog.discourse.group/t/exercice-sur-les-graphes-en-prolog/1654 "2019-12-23T12:09:20Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 23, 2019, 2:30pm UTC](https://swi-prolog.discourse.group/t/exercice-sur-les-graphes-en-prolog/1654/12 "2019-12-23T14:30:12Z")

</div>

The first part of the problem is to get next related node in the graph, this can be done using

```prolog
suivant_2(X, Y) :- arc(X, Y).
suivant_2(X, Y) :- arc(X, Z), suivant_2(Z, Y).

```

Example queries

```prolog
?- suivant_2(a,R).
R = o .

?- suivant_2(l,R).
R = v ;
R = l ;
R = v ;
R = l ;
R = v ;
...

?- suivant_2(v,R).
R = l .

?- suivant_2(y,R).
R = v ;
R = o ;
R = l ;
R = v ;
R = l ;
R = v ;
...

```

Obviously there needs to be a change to stop the back and forth between `l` and `v` but that is easy to fix, e.g. [member/2](https://www.swi-prolog.org/pldoc/man?predicate=member/2). Yes that means the code now needs to capture the values into a list but you learned that in the [previous](https://swi-prolog.discourse.group/t/fibonacci-reverse-output-list/1651) exercise.

Once you have valid results, then collect them into a list with something from [Finding all Solutions to a Goal](https://www.swi-prolog.org/pldoc/man?section=allsolutions)

---

_[View the full topic](https://swi-prolog.discourse.group/t/exercice-sur-les-graphes-en-prolog/1654)._
