I'm trying to implement an algorithm to find what I call 'guaranteed ancestors' in a directed graph. I have a list of nodes which each can point to zero, one or multiple child nodes.
Below you see an example of a simple graph. I've marked all circles with a unique number.
Let's imagine we're trying to determine which nodes I'm guaranteed to have visited before reaching node 13 starting at node 0.
My thoughts when solving this simple example by hand is starting in node 13 and working my way back, which nodes am I guaranteed to visit no matter which direction I go. The first node I notice obeying this property is node 10, since no matter if I choose to visit node 11 or node 12, then I'm guaranteed to eventually reach node 13. Similarly I can conclude I have to visit node 9 if I want to reach node 13. Working all the way up the graph I conclude that node 13 has node 0, 1, 9, 10 as it's guaranteed anchestors.
I'm not sure what such an algorithm is called, but I'm sure there is a name for this specific search.
Here is the constraints you can assume about my graph.
There is a single defined "head/root" node, which is the only node without any other nodes pointing to it.
The graph is acyclic (Ideally the algorithm would be able to handle cycles too, but I have a different check, verifying that the graph is acyclic, so this is not a must.)
There is no "dead" nodes, eg. nodes which can't be reached from the head/root node.
This has to run on more complicated graphs with up to 500 nodes and many nodes with multiple "parents", which could be connected back and forth. Runtime is a priority as well - I assume we should be able to solve this problem in linear time complexity.
I've tried simplifying the problem to the point where I tried making an algorithm which could determine if a single node was a guaranteed anchestor of another node, which I believe is pretty simple to determine in O(n), however if I want a complete list of all guaranteed anchestors I assume I'd have to run this algorithm for every node, leaving me with O(n^2).
Does anyone know the correct name of the algorithm I'm describing?
Assign a weight of 1 to every edge
Run Dijkstra to find shortest path between head and root.
Assign weight of 2 * ( edge count of graph ) to every edge in path
Run Dijkstra to find cheapest path
Identify edges that are present in both paths. ( they could not be avoided although very expensive )
The nodes at both ends of every edge identified in 5 will be critical - i.e they must ALL be visted by any route between head and root.
Consider an example:
The first Dijkstra run would return a path containing node 1 or 2 ( they both belong on 5 hop paths. The second run would return a path containing the other of those two nodes
This is almost what the definition of an articulation or cut vertex is in an undirected graph. See Biconnected component:
a cut vertex is any vertex whose removal increases the number of connected components.
The difference is that your graph is directed, and that you consider the root also as such a vertex.
So my suggestion is to temporarily consider the graph to be undirected, and to apply a depth-first algorithm to identify such cut vertices, and include the root.
The algorithm is given as pseudo code in the same Wikipedia article. I have rewritten it in JavaScript, so it can be run here for the graph that you have given as example:
function buildAdjacencyList(n, edges) {
// Indexes in adj represent node identifiers.
// Values in adj are lists of neighbors: start out with empty lists
let adj = [];
for (let i = 0; i < n; i++) adj.push([]);
for (let [start, end] of edges) {
adj[start].push(end );
adj[end ].push(start); // make edge bidirectional
}
return adj;
}
function markArticulationPoints(nodes, node, depth) {
node.visited = true;
node.depth = depth;
node.low = depth;
for (let neighborId of node.neighbors) {
let neighbor = nodes[neighborId];
if (!neighbor.visited) {
neighbor.parent = node;
markArticulationPoints(nodes, neighbor, depth + 1);
if (neighbor.low >= node.depth) node.isArticulation = true;
if (neighbor.low < node.low) node.low = neighbor.low;
} else if (neighbor != node.parent && neighbor.depth < node.low) {
node.low = neighbor.depth;
}
}
}
function getArticulationPoints(adj, root) {
// Create object for each node, having meta data for algorithm
let nodes = [];
for (let i = 0; i < adj.length; i++) {
nodes.push({
neighbors: adj[i],
visited: false,
depth: Infinity,
low: Infinity,
parent: -1,
isArticulation: i == root // root is considered articulation point
});
}
markArticulationPoints(nodes, nodes[root], 0); // start DFS algorithm
// Collect articulation points from meta data
let result = [];
for (let i = 0; i < adj.length; i++) {
if (nodes[i].isArticulation) result.push(i);
}
return result;
}
// Build adjacency list for example graph, but with undirected edges
let adj = buildAdjacencyList(14, [
[0, 1],
[1, 2],
[1, 3],
[2, 4],
[2, 5],
[4, 5],
[4, 6],
[3, 7],
[7, 8],
[6, 9],
[8, 9],
[9, 10],
[10, 11],
[10, 12],
[11, 13],
[12, 13]
]);
let result = getArticulationPoints(adj, 0);
console.log("Articluation points:", ...result);
Related
am working on this assignment but am confused on which node will the best first search move next. using Manhattan distance, I found that all the nodes which are directly connected to the starting node have the same distance. my question is that since it is s BFS and am supposed to use Manhattan distance as evaluation function. how will the BFS decide which node to explore next.
const getHeuristic = (row, col) => {
return Math.abs(end.row - row) + Math.abs(end.col - col);
};
const NEIGHBORS = [
[-1, 0],
[0, 1],
[1, 0],
[0, -1],
];
const visitNeighbors = (node) => {
let { row, col } = node;
NEIGHBORS.forEach((neighbor) => {
let r = row + neighbor[0];
let c = col + neighbor[1];
if (checkIndexes(matrix, r, c)) {
//check to see if the neighbors of this node don't cause out of bounds issues. Like edge nodes.
let cost = getHeuristic(r, c);
pQueue.insert({ row: r, col: c, cost });
}
});
};
let begin = {
row: start.row,
col: start.col,
cost: 0,
};
pQueue.insert(begin);
while (pQueue.size() > 0) {
let cell = pQueue.pop();
if (isEquals(cell, end)) {
//handle when end is found. If you want to trace the path, the use a map to keep track of the parent node when you explore its children.
return;
}
visitNeighbors(cell);
}
For Best-first search,
You would use a heuristic function that best estimates the distance to goal from current node. For Manhattan distance, that function could be: Math.abs(goal.row - current.row) + Math.abs(goal.col - current.col). Here you take the different between the row values and column values of node you are currently processing and your goal node and add their absolute values together.
You would then have a priority queue in which you add the neighbors along with heuristic cost to get to them.
You would remove the least costing node from the priority queue based on the heuristic value and explore each of its neighbors and calculate heuristic cost to get to those nodes and push to the priority queue and so on until you reach the end node.
If the estimated distances of nodes are equal, then either node you choose will produce the correct result. Best First Search doesn't guarantee the shortest path. But to answer your question, you don't need to have a tie breaker if they have the same cost, just remove from priority queue, whatever gets removed is still correct. If you need the shortest path, then look into A-Star algorithm.
P.S. for further clarification, ask in the comments, you shouldn't create an answer to ask another question/clarification.
I need some help to figure out union-find problem.
Here is the question.
There's an undirected connected graph with n nodes labeled 1..n. But
some of the edges has been broken disconnecting the graph. Find the
minimum cost to repair the edges so that all the nodes are once again
accessible from each other.
Input:
n, an int representing the total number of nodes.
edges, a list of
integer pair representing the nodes connected by an edge.
edgesToRepair, a list where each element is a triplet representing the
pair of nodes between which an edge is currently broken and the cost
of repearing that edge, respectively
(e.g. [1, 2, 12] means to repear
an edge between nodes 1 and 2, the cost would be 12).
Example 1:
Input: n = 5, edges = [[1, 2], [2, 3], [3, 4], [4, 5], [1, 5]],
edgesToRepair = [[1, 2, 12], [3, 4, 30], [1, 5, 8]]
Output: 20
There are 3 connected components due to broken edges:
[1], [2, 3] and [4, 5]. We can connect these components into a single
component by repearing the edges between nodes 1 and 2, and nodes 1
and 5 at a minimum cost 12 + 8 = 20.
public int minCostRepairEdges(int N, int[][] edges, int[][] edgesToRepair){
int[] unionFind = new int[N+1];
int totalEdges=0;
for(int[] edge : edges){
int ua = edge[0]; //node1
int ub = edge[1]; //node2
unionFind[ua] = ub;
totalEdges++;
}
//change unionFind for some broken edge
for(int[] broken : edgesToRepair){
int ua = Find(unionFind, broken[0]);
int ub = Find(unionFind, broken[1]);
if(ua == ub){
unionFind[ua] = 0;
}
}
Arrays.sort(edgesToRepair, (a,b)->(a[2]-b[2]));
int cost=0;
for(int[] i : edgesToRepair){
int ua = Find(unionFind, i[0]);
int ub = Find(unionFind, i[1]);
if(ua != ub){
unionFind[ua] = ub;
cost += i[2];
totalEdges++;
}
}
return edgesToRepair==N-1 ? cost : -1;
}
public int find(int[] uf, int find){
if(uf[find]==0) return find;
uf[find] = find(uf, uf[find]);
return uf[find];
}
And Above is my code so far.
My idea is that
First Adding all edges (given edges[][]) to UnionFind and then Update it based on given edgesToRepair Info. (if edge was broken then going to update it in union -find)
Then just try to do basic union-find algorithm to connect two nodes with minimum cost.
Any wrong approach in here?
First, I have trouble updating unionFind when it was broken.
I can't figure out how to handle unbroken edges between two nodes.
Any advice would be helpful.
You're supposed to use Kruskal's algorithm to find a minimum cost spanning tree consisting of the existing edges and broken (repaired) edges:
https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
Just consider the existing edges to have 0 cost, while the broken edges have their repair cost.
I have a list of nodes which belong in a graph. The graph is directed and does not contain cycles. Also, some of the nodes are marked as "end" nodes. Every node has a set of input nodes I can use.
The question is the following: How can I sort (ascending) the nodes in the list by the biggest distance to any reachable end node? Here is an example off how the graph could look like.
I have already added the calculated distance after which I can sort the nodes (grey). The end nodes have the distance 0 while C, D and G have the distance 1. However, F has the distance of 3 because the approach over D would be shorter (2).
I have made a concept of which I think, the problem would be solved. Here is some pseudo-code:
sortedTable<Node, depth> // used to store nodes and their currently calculated distance
tempTable<Node>// used to store nodes
currentDepth = 0;
- fill tempTable with end nodes
while( tempTable is not empty)
{
- create empty newTempTable<Node node>
// add tempTable to sortedTable
for (every "node" in tempTable)
{
if("node" is in sortedTable)
{
- overwrite depth in sortedTable with currentDepth
}
else
{
- add (node, currentDepth) to sortedTable
}
// get the node in the next layer
for ( every "newNode" connected to node)
{
- add newNode to newTempTable
}
- tempTable = newTempTable
}
currentDepth++;
}
This approach should work. However, the problem with this algorithm is that it basicly creates a tree from the graph based from every end node and then corrects old distance-calculations for every depth. For example: G would have the depth 1 (calculatet directly over B), then the depth 3 (calculated over A, D and F) and then depth 4 (calculated over A, C, E and F).
Do you have a better solution to this problem?
It can be done with dynamic programming.
The graph is a DAG, so first do a topological sort on the graph, let the sorted order be v1,v2,v3,...,vn.
Now, set D(v)=0 for all "end node", and from last to first (according to topological order) do:
D(v) = max { D(u) + 1, for each edge (v,u) }
It works because the graph is a DAG, and when done in reversed to the topological order, the values of all D(u) for all outgoing edges (v,u) is already known.
Example on your graph:
Topological sort (one possible):
H,G,B,F,D,E,C,A
Then, the algorithm:
init:
D(B)=D(A)=0
Go back from last to first:
D(A) - no out edges, done
D(C) = max{D(A) + 1} = max{0+1}=1
D(E) = max{D(C) + 1} = 2
D(D) = max{D(A) + 1} = 1
D(F) = max{D(E)+1, D(D)+1} = max{2+1,1+1} = 3
D(B) = 0
D(G) = max{D(B)+1,D(F)+1} = max{1,4}=4
D(H) = max{D(G) + 1} = 5
As a side note, if the graph is not a DAG, but a general graph, this is a variant of the Longest Path Problem, which is NP-Complete.
Luckily, it does have an efficient solution when our graph is a DAG.
I have a situation where the predecessors of a node must be visited before the node is visited. So, here is the code for that:
nodeQ.Enqueue(rootNode);
while(!nodeQ.Empty())
{
node = nodeQ.Dequeue();
ForEach(var predecessor in node.Predecessors)
{
if(predecessor is not visited)
{
//put the node back into the queue
nodeQ.Enqueue(node);
skip = true;
break;
}
}
if(skip)continue;
Visit(node)
foreach(var successor in node.Successors)
{
if(successor is not already visited)
{
nodeQ.Enqueue(successor);
}
}
}
The above algorithm will be ok for linear control flow graphs without cycles (read: loops)
The normal BFS traversal doesn't ensure that the predecessors of a node are visited before the node itself.
Example CFG:
The Normal BFS traversal will be:
0, 1 , 2 , 3 , 12, 4, 5, 9, 10, 11, 8 , 6, 7
However, I want the order to be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 which can be acheived by the small modification that is there in my code shown in the beginning.
However, this modification will cause endless skipping of blocks when there are loops involved.
Example CFG where this can fail:
In this scenario, my code will endlessly postpone Visiting of Nodes 1, 2, 3
So, I was looking for a way of traversal that ensures the traversal of nodes of a CFG (with or without loops) in such a way that the predecessors of a node are visited before the node itself.
I was thinking of identifying back-edge i.e, checking if a node N is a Dominator of its predecessor P, then P->N is a backedge and there is no need to consider P as a predecessor of node N. However this doesnt seem to work as node N doesnt always have to dominate node P.
I solved this problem by first finding Dominators, Creating a Dominator Tree and then traverse the Tree in DFS pre-order.
For others who come to this question seeking CFG and dominator calculation guidance, it might be useful to check out the IBM WALA tools. I'm finding a lot of useful information here for my particular quest along these lines.
In my particular case, the graph is represented as an adjacency list and is undirected and sparse, n can be in the millions, and d is 3. Calculating A^d (where A is the adjacency matrix) and picking out the non-zero entries works, but I'd like something that doesn't involve matrix multiplication. A breadth-first search on every vertex is also an option, but it is slow.
def find_d(graph, start, st, d=0):
if d == 0:
st.add(start)
else:
st.add(start)
for edge in graph[start]:
find_d(graph, edge, st, d-1)
return st
graph = { 1 : [2, 3],
2 : [1, 4, 5, 6],
3 : [1, 4],
4 : [2, 3, 5],
5 : [2, 4, 6],
6 : [2, 5]
}
print find_d(graph, 1, set(), 2)
Let's say that we have a function verticesWithin(d,x) that finds all vertices within distance d of vertex x.
One good strategy for a problem such as this, to expose caching/memoisation opportunities, is to ask the question: How are the subproblems of this problem related to each other?
In this case, we can see that verticesWithin(d,x) if d >= 1 is the union of vertices(d-1,y[i]) for all i within range, where y=verticesWithin(1,x). If d == 0 then it's simply {x}. (I'm assuming that a vertex is deemed to be of distance 0 from itself.)
In practice you'll want to look at the adjacency list for the case d == 1, rather than using that relation, to avoid an infinite loop. You'll also want to avoid the redundancy of considering x itself as a member of y.
Also, if the return type of verticesWithin(d,x) is changed from a simple list or set, to a list of d sets representing increasing distance from x, then
verticesWithin(d,x) = init(verticesWithin(d+1,x))
where init is the function that yields all elements of a list except the last one. Obviously this would be a non-terminating recursive relation if transcribed literally into code, so you have to be a little bit clever about how you implement it.
Equipped with these relations between the subproblems, we can now cache the results of verticesWithin, and use these cached results to avoid performing redundant traversals (albeit at the cost of performing some set operations - I'm not entirely sure that this is a win). I'll leave it as an exercise to fill in the implementation details.
You already mention the option of calculating A^d, but this is much, much more than you need (as you already remark).
There is, however, a much cheaper way of using this idea. Suppose you have a (column) vector v of zeros and ones, representing a set of vertices. The vector w := A v now has a one at every node that can be reached from the starting node in exactly one step. Iterating, u := A w has a one for every node you can reach from the starting node in exactly two steps, etc.
For d=3, you could do the following (MATLAB pseudo-code):
v = j'th unit vector
w = v
for i = (1:d)
v = A*v
w = w + v
end
the vector w now has a positive entry for each node that can be accessed from the jth node in at most d steps.
Breadth first search starting with the given vertex is an optimal solution in this case. You will find all the vertices that within the distance d, and you will never even visit any vertices with distance >= d + 2.
Here is recursive code, although recursion can be easily done away with if so desired by using a queue.
// Returns a Set
Set<Node> getNodesWithinDist(Node x, int d)
{
Set<Node> s = new HashSet<Node>(); // our return value
if (d == 0) {
s.add(x);
} else {
for (Node y: adjList(x)) {
s.addAll(getNodesWithinDist(y,d-1);
}
}
return s;
}