views:

289

answers:

2

I have a graph with four nodes, each node represents a position and they are laid out like a two dimensional grid. Every node has a connection (an edge) to all (according to the position) adjacent nodes. Every edge also has a weight.

Here are the nodes represented by A,B,C,D and the weight of the edges is indicated by the numbers:

A     100     B

120         220

C     150     D

I want to structure a container and an algorithm that switches the nodes sharing the edge with the highest weight. Then reset the weight of that edge. No node (position) can be switched more than once each time the algorithm is executed.

For example, processing the above, the highest weight is on edge BD, so we switch those. Since no node can be switched more than once, all edges involved in either B or D is reset.

A             D

120         

C             B

Then, the next highest weight is on the only edge left, switching those would give us the final layout: C,D,A,B.

I'm currently running a quite awful implementation of this. I store a long list of edges, holding four values for the nodes they are (potentially) connected to, a value for its weight and the position for the node itself. Every time anything is requested, I loop through the entire list.

I'm writing this in C++, could some parts of the STL help speed this up? Also, how to avoid the duplication of data? A node position is currently in five objects. The node itself that is there and the four nodes indicating a connection to it.

In short, I want help with:

  • Can this be structured in a way so that there is no data duplication?
  • Recognise the problem? If any of this has a name, tell me so I can google for more info on the subject.
  • Fast algorithms are always nice.
A: 

If you have bigger graphs look into the boost graph library. It gives you good data structures for graphs and basic iterators for different types of graph traversing

Janusz
+2  A: 

As for names, this is a vertex cover problem. Optimal vertex cover is NP-hard with decent approximation solutions, but your problem is simpler. You're looking at a pseudo-maximum under a tighter edge selection criterion. Specifically, once an edge is selected every connected edge is removed (representing the removal of vertices to be swapped).

For example, here's a standard greedy approach:

0) sort the edges; retain adjacency information
while edges remain:
1) select the highest edge
2) remove all adjacent edges from the list
endwhile

The list of edges selected gives you the vertices to swap.
Time complexity is O(Sorting vertices + linear pass over vertices), which in general will boil down to O(sorting vertices), which will likely by O(V*log(V)).

The method of retaining adjacency information depends on the graph properties; see your friendly local algorithms text. Feel free to start with an adjacency matrix for simplicity.

As with the adjacency information, most other speed improvements will apply best to graphs of a certain shape but come with a tradeoff of time versus space complexity.

For example, your problem statement seems to imply that the vertices are laid out in a square pattern, from which we could derive many interesting properties. For example, that system is very easily parallelized. Also, the adjacency information would be highly regular but sparse at large graph sizes (most vertices wouldn't be connected to each other). This makes the adjacency matrix give a high overhead; you could instead store adjacency in an array of 4-tuples as it would retain fast access but almost entirely eliminate overhead.

Chad Wellington
"Edge coloring assigns a color to each edge so that no two adjacent edges share the same color" (http://en.wikipedia.org/wiki/Graph_coloring). Seems to me like Im only interested in two "colors", handled and not-handled. In what way should the "adjacent edges doesnt share color" problem be considered?
mizipzor
Ah, you're right, my hazy memory grabbed the wrong term. I'll correct the post.I was thinking of the operation of "coloring" where you mark finished portions, as you correctly noted-- you only care about usable and non-usable.
Chad Wellington