- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)We define as diameter the greatest of the shortest paths of a graph.
We are given a graph $G=(V,E)$.
I want to write an algorithm that finds the diameter of the graph $G$.
I have tried the following:
Could you tell me if my idea is right? (Thinking)
We are given a graph $G=(V,E)$.
I want to write an algorithm that finds the diameter of the graph $G$.
I have tried the following:
Code:
Diam(G)
// Let W be a nxn array with the weight edges of G
1. FLOYD-WARSHALL(W)
2. max=d_{11}
3. for i=1 to n
4. for j=1 to n
5. if (d_(ij)>max)
6. max=d_(ij)
7. return max
FLOYD-WARSHALL(W)
1. n=W.rows
2. D^(0)=W
3. for k=1 to n
4. let D^(k)=(d_(ij)^(k)) be a new nxn matrix
5. for i=1 to n
6. for j=1 to n
7. d_(ij)^(k)=min(d_(ij)^((k-1)), d_(ik)^((k-1))+d_(kj)^(k-1))
8. return D^(n)
Could you tell me if my idea is right? (Thinking)