2

Suppose we have given an undirected weighted graph and a source and destination node we need to disconnect the source and destination node by removing the edges and the cost of removing the edge is the weight for an edge. We need to minimize the cost to disconnect two nodes.

For Example

enter image description here

here to disconnect the 0 node and 6 node it required 5 min cost

2 Answers2

1

With Answer Set Programming you can efficiently solve NP problems declaratively.

Given an instance file instance.lp to describe your graph:

edge(0,1,5).
edge(1,2,3).                             
edge(2,5,3).                   
edge(5,6,4).
edge(1,3,4).           
edge(3,4,5).          
edge(4,6,6).                                                   
                                                      
source(0).                      
sink(6). 

and an encoding.lp that describes your problem:

%% chose a subset of the edges to remove 
{removed(X,Y,W) : edge(X,Y,W)}.
            
%% source is reachable 
reach(X) :- source(X).
%% going through edges is reachable if the edge ws not removed 
reach(Y) :- reach(X), edge(X,Y,W), not removed(X,Y,W).
%% handling bidirectional edges 
reach(Y) :- reach(X), edge(Y,X,W), not removed(Y,X,W).
                                                                                                                       
%% it is not allowed to reach the sink                                                                                 
:- sink(X), reach(X).                                                                                                  
                                                                                                                       
%% minimize the number of removed edges weighted by W                                                                  
#minimize {W,X,Y : removed(X,Y,W)}.                                                                                    
                                                                                                                       
%% show only removed edges                                                                                             
#show removed/3. 

The call clingo encoding.lp instance.lp (available at https://potassco.org/ ) produces the output:

clingo version 5.5.1
Reading from encoding.lp ...
Solving...
Answer: 1
removed(5,6,4) removed(4,6,6)
Optimization: 10
Answer: 2
removed(5,6,4) removed(1,3,4)
Optimization: 8
Answer: 3
removed(0,1,5)
Optimization: 5
OPTIMUM FOUND

The last answer being the optimal solution to your problem. You can also simply copy the contents of the two files into your browser textfield to try it out here (with reduced performance).

Max Ostrowski
  • 575
  • 1
  • 3
  • 15
0
LOOP
   Use Dijkstra to find min path from src to dst
   If no path found
      STOP
   Remove cheapest link on path
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • It will not give me min cost in the case of the bridge in the graph by just removing the bridge we can disconnect the two-node and its not necessary bridge edge would be a minimum of one – Vishal Jain Jul 17 '21 at 19:10
  • This is called a greedy algorithm. It is not guaranteed to find the optimum. But it usually will, it is fast and easy to implement. For real world problems, it is the way to go – ravenspoint Jul 17 '21 at 19:33
  • If we are not getting an optimal solution then we can just do a dfs find all the paths and from each path remove the least one why go with Dijkstra, but here we need to find minimum cost every time with dfs or Dijkstra it will not work – Vishal Jain Jul 17 '21 at 20:08
  • check I have added an example where dfs or Dijkstra will give us the incorrect result, with dfs or Dijkstra we will get 7 as result but the answer is 5 – Vishal Jain Jul 17 '21 at 20:28
  • As I explained, the algorithm I posted in not guaranteed to always find the optimum solution. – ravenspoint Jul 18 '21 at 11:47