I would like to make a graph algorithm that updates/computes
the value of a node f(n)
as a function of each of the f(n)
values of
neighboring nodes.
More formally,
f(n) = max(f(n),max_i(f(n_i))), where i from neighbor 1 to neighbor k.
I can visualize a few ways of doing so, however I do not know to what extend they are optimal.
Can anyone please give suggestions and commentaries (whether do you think your suggestion is optimal) or suggest any existent graph algorithm I can adapt?
Use the len() and list() functions together with the . neighbors() method to calculate the total number of neighbors that node n in graph G has. After iterating over all the nodes in G , return the set nodes .
In a graph, the neighbours of a node consist in the set of nodes that are connected to this node up to a certain distance, i.e., the number of steps between the source node and its neighbours. In weighted graphs, one can also consider the neighbours up to a certain maximal weight.
Steps of Prim's AlgorithmSelect any vertex, say v1 of Graph G. Select an edge, say e1 of G such that e1 = v1 v2 and v1 ≠ v2 and e1 has minimum weight among the edges incident on v1 in graph G. Now, following step 2, select the minimum weighted edge incident on v2. Continue this till n–1 edges have been chosen.
Explanation: The Breadth First Search Algorithm searches the nodes on the basis of level. It takes a node (level 0), explores it's neighbors (level 1) and so on. Explanation: The Breadth First Search explores every node once and every edge once (in worst case), so it's time complexity is O(V + E).
Claims:
In each Strongly Connected Component V
in the graph, the values of all vertices in this SCC have the same final score.
"Proof" (guideline): by doing propogation in this SCC, you can iteratively set all the scores to the maximal value found in this SCC.
In a DAG, the value of each vertex is max{v,parent(v) | for all parents of v}
(definition) and the score can be found within a single iteration from the start to the end.
"Proof" (guideline): There are no "back edges", so if you know the final values of all parents, you know the final value of each vertex. By induction (base is all sources) - you can get to the fact that a single iteration is enough to determine the final score.
Also, it is known that the graph G'
representing the SCC of a
graph g
is a DAG.
From the above we can derive a simple algorithm:
G'
. Note that G'
is a DAG.V
: set f'(V) = max{v | v in V}
(intuitively - set the value of each SCC as the max value in this SCC).G'
.G'
according to its parents.f(v) = f'(V)
(where v is in the SCC V)Complexity:
G'
is O(V+E)
O(V+E)
O(V+E)
O(V+E)
So the above algorithm is linear in the size of the graph - O(V+E)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With