tags:

views:

644

answers:

8

I'm concerned that this might be working on an NP-Complete problem. I'm hoping someone can give me an answer as to whether it is or not. And I'm looking for more of an answer than just yes or no. I'd like to know why. If you can say,"This is basically this problem 'x' which is/is not NP-Complete. (wikipedia link)"

(No this is not homework)

Is there a way to determine if two points are connected on an arbitrary non-directed graph. e.g., the following

Well
  |
  |
  A
  |
  +--B--+--C--+--D--+
  |     |     |     |
  |     |     |     |
  E     F     G     H
  |     |     |     |
  |     |     |     |
  +--J--+--K--+--L--+
                    |
                    |
                    M
                    |
                    |
                  House

Points A though M (no 'I') are control points (like a valve in a natural gas pipe) that can be either open or closed. The '+'s are nodes (like pipe T's), and I guess the Well and the House are also nodes as well.

I'd like to know if I shut an arbitrary control point (e.g., C) whether the Well and House are still connected (other control points may also be closed). E.g., if B, K and D are closed, we still have a path through A-E-J-F-C-G-L-M, and closing C will disconnect the Well and the House. Of course; if just D was closed, closing only C does not disconnect the House.

Another way of putting this, is C a bridge/cut edge/isthmus?

I could treat each control point as a weight on the graph (either 0 for open or 1 for closed); and then find the shortest path between Well and House (a result >= 1 would indicate that they were disconnected. There's various ways I can short circuit the algorithm for finding the shortest path too (e.g., discard a path once it reaches 1, stop searching once we have ANY path that connects the Well and the House, etc.). And of course, I can also put in some artificial limit on how many hops to check before giving up.

Someone must have classified this kind of problem before, I'm just missing the name.

+3  A: 

not NP-complete, solved with a well-known solution - Dijkstra's Algorithm

Steven A. Lowe
+5  A: 

See http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm , you're one stop shop for all graph related problems. I believe your problem is in fact solvable in quadratic time.

Nick Gebbie
Why do you say that CodeSlave is "one stop shop"?
Svante
'cause I can do anything... of course some things take a little longer or cost more than other things - but given sufficient resources I can do it.
CodeSlave
+3  A: 

To me it seems like you are on to a solution, but it's possible I misunderstood the problem. If you do like you say, and give the closed edges 1 as weight, you can just apply Dijkstra's algorithm, http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm. This should solve your problem in O(E*lg(V))

torb
+2  A: 

The problem of finding the shortest path isn't NP-complete. It's called the Shortest Path Problem (originally enough) and there are algorithms for solving many different variations of it.

The problem of determining if two nodes are connected isn't NP-complete either. You can use a depth first search starting at either node to determine if it is connected to the other node.

Bill the Lizard
+8  A: 

Your description seems to indicate that you are just interested in whether two nodes are connected, not finding the shortest path.

Finding if two nodes are connected is relatively easy:

Create two sets of nodes:  toDoSet and doneSet
Add the source node to the toDoSet 
while (toDoSet is not empty) {
  Remove the first element from toDoList
  Add it to doneList
  foreach (node reachable from the removed node) {
    if (the node equals the destination node) {
       return success
    }
    if (the node is not in doneSet) {
       add it to toDoSet 
    }
  }
}

return failure.

If you use a hash table or something similar for toDoSet and doneSet, I believe this is a linear algorithm.

Note that this algorithm is basically the mark portion of mark-and-sweep garbage collection.

David Norman
you should to check that the node to add isn't in todoset as well as checking if it's in doneset
FryGuy
If toDoSet is something other than a vector, then adding will do the check if it's already there. I'll update the answer.
David Norman
It's worth noting that this is simply a breadth first search. Either this or a DFS will work in O(n) time (where n is the number of vertices in the graph).
Nick Johnson
David, you are right, I could model this as whether the nodes are connected or not (hence, I'll give you a +1). However, because the control points are on the edges (and changes often), its easier for me to model it as a shortest path (and make a closed control point VERY expensive).
CodeSlave
The result is basically the same for both the connected and shortest path, and similar in run time.
CodeSlave
+3  A: 

You don't need Dijkstra's algorithm for this problem, as it uses a Heap which isn't needed and introduces a factor of log(N) to your complexity. This is just breadth first search - don't include the closed edges as edges.

Gwildore
A: 

Assuming that you have an adjacency matrix:

bool[,] adj = new bool[n, n];

Where bool[i,j] = true if there is an open path between i and j and bool[i,i] = false.

public bool pathExists(int[,] adj, int start, int end)
{
  List<int> visited = new List<int>();
  List<int> inprocess = new List<int>();
  inprocess.Add(start);

  while(inprocess.Count > 0)
  {
    int cur = inprocess[0];
    inprocess.RemoveAt(0);
    if(cur == end)
      return true;
    if(visited.Contains(cur))
      continue;
    visited.Add(cur);
    for(int i = 0; i < adj.Length; i++)
      if(adj[cur, i] && !visited.Contains(i) && !inprocess.Contains(i))
        inprocess.Add(i);
  }
  return false;
}
Jason Lepack
A: 

Dijkstra's is overkill!! Just use breadth first search from A to search for the node you want to reach. If you can't find it, it's not connected. Complexity is O(nm) for each search, which is less then Dijkstra.

Somewhat related is the max-flow/min-cut problem. Look it up, it might be relevant to your problem.

rutger