0

I have come across a problem which goes something like this.

We are given an undirected graph where each edge has a value. Now the extra constraint is that one can not move from a higher value edge to a lower value edge. One always has to move from lower to higher value.

Pictorially the problem can be depicted as

                                   1
                               1 /   \ 1
                                /     \
                               3       7
                            2 / \  3   
                             /   \
                            4     5

So here we can move from 7 -> 1 -> 3 -> 4 but not 4 -> 3 -> 1. So in this graph we are asked to find out the strongly connected components like (1, 3, 7) in here.


I have tried to use Kosaraju's Algorithm with the constraint like this.

for v in adj[u]:
    if not visited[v] and v.edgeValue >= u.edgeValue:
        do work here

But the logic, I think, is wrong and I can not find out where it is failing. Can someone point out the fault and show some kind of pseudocode?

Thank you.

  • 2
    We can go 4->3 and 3->1 separately, right? If so, it's not clear to me what "strong component" means. – David Eisenstat Nov 12 '15 at 19:51
  • If I am not wrong, you are asking how to find ALL strongly connected component which all nodes can go to any other nodes within the component, which means that all edge in the component has same weight, and how can we do that, right? – shole Nov 13 '15 at 08:21

1 Answers1

0

Well, in your constraint, the direction your allowed to go depends on your history of travel. That makes connected component have a bit different meaning. I am assuming that by strongly connected component you mean that for a subgraph from any vertex, any other vertex of that subgraph can be reached. Since your constraint specifies that you cannot move from lower valued edge to higher value, that would mean that any subgraph that is strongly connected, contains a tree with equal weighted edges.

So in example above, you would have (4,3)(3,5)(3,1,7) as one particular partition of the graph into strong components. You can get those by doing a simple DFS or BFS while including only edges of a specific weight.

Nicko Po
  • 727
  • 5
  • 21