1

I'm new to answer set programming and could use some help. I've been reading this, but still could use some help. How would I use answer set programming to tell if a graph is strongly connected?

My brainstorming:

  • Graph represented by nodes and edges (ie; node(1..2), edge(1,2), and edge(2,1)).

  • Now I need rule "strong() :- ......" that is true if the graph is strongly connected.

  • A graph is strongly connected if you can start at any node and reach any other node by following the edges in the direction(s) they point.

  • So my program needs to take each node X and go along the directed edges to try and reach every other node. True if it reaches every other node, False otherwise.

Strong() :- ?

MoreFoam
  • 624
  • 1
  • 6
  • 25
  • You dont need the adverb "strongly". The definition is "An undirected graph is **connected** when it has at least one vertex and there is a path between every pair of vertices." See also: https://en.wikipedia.org/wiki/Connectivity_%28graph_theory%29#Connected_graph –  Oct 02 '19 at 14:59

1 Answers1

0

First, unless your graph is directed, you have to get the symmetric edges:

edge(X,Y):- edge(Y,X).

Then, you need to tell that two nodes are connected if there is a path between them:

connected(X,Y):- edge(X,Y).
connected(X,Z):- edge(X,Y) ; connected(Y,Z).

Now, strong holds if for all pairs of nodes, they are connected:

strong:- connected(X,Y): edge(X,_), edge(_,Y).

An alternative version could be:

not_strong:- not connected(X,Y) ; edge(X,_) ; edge(_,Y).
strong:- not not_strong.

(Tested with clingo 4.5.4)

aluriak
  • 5,559
  • 2
  • 26
  • 39