DOT Graph/Graphviz -- getting top nodes and edges based on edge weight - graphviz

What's the best way to get the top X percentage of edges (based on the weight of the edge) from a DOT graph. I couldn't find any Python function that already does this.
Edge weight is basically the content in the label.
123 -> 456[label="10s" fontsize=12 penwidth=1.0 ]

Related

minimum RGB path

You are given a directed graph G = (V, E) weighted edges. Nodes are colored red, green and blue.
A path from s to v is called colorful if it contains red green and a blue node.
find the shortest path between s and every v, in condition that the path should be colorful.
If there is more than one path that is colorful find the minimum between them.
If there is no such a path return false.
I'm not sure how to get started on this. Any help is greatly appreciated!
As you walk along a path from s to v, go from one "state" to another, where the "state" includes both the node you are on and which colors of nodes you've been on in your journey (7 possible combinations). Your state determines which paths from your current position are valid solutions.
From your original graph, make (or imagine) a graph of accessible states, where an edge represents the ability to transition from one state to the next. This graph will have up to 7 vertices for each of the original graph vertices.
Now use Dijkstra's algorithm to find the shortest path from (s,red) (assuming s is red) to (v,red+green+blue)

Removing max edges while keeping minimum distance

Suppose we have a graph with vertices from 1 to n.The graph is undirected and the starting point is 1 and we have path from 1 to any other vertex.We also have positive weight on each edge and there are two types of edges - black and red.
The black edges are in the form (1,x) where x is a vertex and red edges can be any pair (x,y) .My question is how can I find the maximum number of black edges I can remove so that the minimal distance from 1 to any other vertex is preserved?
Something like this. Using your favorite path-finding algorithm, find a shortest path from 1 to each other vertex using a cost function of {total weight, number of black edges}. This would produce paths that prefer red edges, other things equal.
You can now remove all black edges that don't belong to any path.

the shortest even path to minimum weight perfect matching

Given an edge weighted undirected graph and two vertices s and t, weights are non-negative.
Shortest Even Path Problem is to find an s,t-path P having an even number of edges whose total weight is as small as possible. How to reduce Shortest Even Path Problem to minimum weight perfect matching problem
Start with the given graph, imagine painting the nodes blue, and call it Gblue. It has nodes, including sblue and tblue, and undirected edges like ablue <-> bblue.
Make a copy of the graph, paint its nodes green and call it Ggreen.
Now reconnect all of the edges, so that ablue<->bblue and agreen<->bgreen (which have equal weights) become ablue<->bgreen and agreen<->bblue (which have the same weights).
In this combined graph, every edge is between a blue node and a green node, so every path alternates between blue and green. So every path from sblue that has a even number of steps, will end on a green node.
Now on this combined graph, seek the minimum-weight path from sblue to tgreen.
Finally, remove the subscripts.

Path with the minimum number of alterations in graph with colored edgest

I have a directed graph with colored edges (red & blue) that may contain cycles. The question is to write an algorithm given two vertices (s,t) that finds the path with the minimal number of color changes between s and t (if such path exists).
I have found a solution using a variation of Dijkstra (I created a new graph where each vertex correspond to an edge of the previous graph, and contains the color of the edge. For example: if (1,2) is an edge in the old graph, then (1/2) is a vertex in the new one. I connected "adjacent edges" vertices, and edges in the new graph that change color got a weight of 1, where same color transition is 0).
I am looking for a solution in linear time (of V and E). The above one uses VxE edges in the new graph.
Is there such solution to find the minimal path?
First phase: Reduction to the shortest path problem.
For every node i we create two nodes i_red and i_blue.
For every blue edge i->j we create two edges i_red->j_blue with weight 1 and i_blue->j_blue with weight 0.
We handle red edges in similar fashion.
We also need a start node which is connected with start_red and start_blue with connection weight of 0.
Similar to the target node, which is connected with target_red and target_blue with weight 0-connections.
Now, search for the shortest path from newly created start node to the newly created target node. There are twice as many nodes and twice as many edges as in the original graph, so the reduction is linear.
After you reduced the problem to the shortest path search, you could do the following:
Step: use only edges with weight 0, treat the graph as undirected one and with help of bfs you can find all components in this 0-edge-graph in linear time.
Step: run bfs on the graph where the components from the prior step are glued together as super-nodes, so all edges have weight 1 and bfs will find the shortest path.
Obviously all three parts of this algorithm (bfs in 0-edge-graph, glueing the components to super-nodes and the bfs in the resulting graph) run in linear time.

Shortest paths with 2 constraints (Weight and Colour)

Input: We have a directed graph G=(V,E) and each edge has a weight and a colour {red,green}. We are also given a starting node s.Problem/Algorithm: Can we find for all u edges of G, the shortest paths s-u with at most k red edges ? First approach: We save for each node the shortest path with 0,1...k red edges. We modify Dijkstra's algorithm and depending on the colour of the edges we are looking into, we update the distances respectively. This approach fails due to its complexity. Second approach: We make k copies of G graph (G1,G2 ...Gk+1). In order to utilise the k red edges constraint, while we are searching for shortest paths with Dijkstra, every time we "meet" a red edge {ui,vi} in Gi, we connect ui with vi+1 in Gi+1. Because Gk+1 doesn't have any red edges, we can only reach Gk+1 with at most k edges.But it fails. For example with k=2 if a 2 red edges shortest path is found to X node then will not take into consideration a heavier path with less red edges which could lead to an undiscovered node. (If i had enough reputation i could post an image as example). Any ideas ?
I think your approaches are actually equivalent, provided that for approach #1, you record only the shortest distance to each node for each number of red edges used -- you don't need to record the entire path (just as you don't need to record it for ordinary Dijkstra on an ordinary shortest path problem)
Also this approach is sound. In particular, your reasoning that approach #2 is faulty is itself wrong: for any node X in the original graph, there is no single corresponding node X in the new graph; instead there are separate vertices for each number of red edges used. So the two paths "to X" you are considering are not actually to the same node: one is to (X, 2 red edges used) and one is to e.g. (X, 1 red edge used). Then you can use a single Dijkstra run to calculate shortest paths to all k+1 copies of every vertex (i.e. to the vertices (v, i red edges used) for each 0 <= i <= k and for each v in V(G)), and return the lowest. (I'm assuming here that when you wrote "Can we find for all u edges of G, the shortest paths s-u", you meant "for all nodes u of G, the shortest paths s-u".)
Finally, you need to make sure that for any red edge {u, v} in G, you delete the corresponding edge {ui, vi} for all Gi (as well as add in the edge {ui, vi+1}). You probably intended this, but you weren't explicit about it.

Resources