Find the greatest of the shortest paths

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on implementing an algorithm to find the diameter of a graph using the Floyd-Warshall algorithm. The proposed algorithm, Diam(G), initializes a matrix of edge weights and iterates through it to determine the maximum shortest path. The time complexity of the algorithm is confirmed to be Θ(n³), primarily due to the Floyd-Warshall algorithm's inherent complexity. Participants emphasize the importance of understanding the algorithm's logic rather than just the code.

PREREQUISITES
  • Understanding of graph theory concepts, specifically graph diameter
  • Familiarity with the Floyd-Warshall algorithm for finding shortest paths
  • Knowledge of time complexity analysis in algorithms
  • Ability to interpret and analyze algorithmic pseudocode
NEXT STEPS
  • Study the Floyd-Warshall algorithm in detail, including its implementation and applications
  • Learn about graph diameter and its significance in network analysis
  • Explore alternative algorithms for finding graph diameters, such as Dijkstra's algorithm
  • Investigate optimizations for the Floyd-Warshall algorithm to improve performance
USEFUL FOR

Computer scientists, software engineers, and students studying algorithms and graph theory who are interested in optimizing graph-related computations.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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:
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)
 
Technology news on Phys.org
evinda said:
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:
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)

Hello evinda.

I apologize in advance that my post is not helpful in answering your question.

I just want to suggest that if you want your algorithm checked then you should actually put your algorithm rather than the code. Checking someones code is an arduous task.
 
I have an opinion about your code, but could you explain why you think it computes the graph diameter (without explaining the details of the Floyd–Warshall algorithm)?
 
Evgeny.Makarov said:
I have an opinion about your code, but could you explain why you think it computes the graph diameter (without explaining the details of the Floyd–Warshall algorithm)?

The Floyd-Warshall algorithm returns the matrix $D^{(n)}=(d_{ij}^{(n)})$ of shortest-path weights.
Isn't the diameter the maximum of the shortest-path weights?
Or am I wrong?
 
You are right. But I agree with caffeinemachine that a word description like this is easier to understand then a code.
 
Evgeny.Makarov said:
You are right. But I agree with caffeinemachine that a word description like this is easier to understand then a code.

Nice! (Happy)

The time complexity of the algorithm [m]Diam(G)[/m] is $\Theta(n^3)$, since:

  • the line 1, where we call FLOYD-WARSHALL(W), requires $\Theta(n^3)$ time,
  • the line 2 requires $O(1)$ time,
  • the lines 3-6 require $O(n^2)$ time, since each for-loop requires $O(n)$ time and the lines $5,6$ require constant time and finally,
  • the line 7 requires contant time.

Does this suffice or do I have to show also that the Floyd-Warshall algorithm requires $O(n^3)$ time?

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)
 
evinda said:
the lines 3-6 require $O(n^2)$ time, since each for-loop requires $O(n)$ time and the lines $5,6$ require constant time
I wouldn't say that the outer loop requires $O(n)$ time. When I am talking about a loop, I include its body, and by saying that a loop takes a certain time I mean that it takes that time for the whole loop to finish. So the outer loop takes time $O(n^2)$ because its body (the inner loop) executes $n$ times and each execution takes time $O(n)$.

evinda said:
Does this suffice or do I have to show also that the Floyd-Warshall algorithm requires $O(n^3)$ time?
This is a well-known fact, but it depends on the requirements of your course.
 
Evgeny.Makarov said:
I wouldn't say that the outer loop requires $O(n)$ time. When I am talking about a loop, I include its body, and by saying that a loop takes a certain time I mean that it takes that time for the whole loop to finish. So the outer loop takes time $O(n^2)$ because its body (the inner loop) executes $n$ times and each execution takes time $O(n)$.

This is a well-known fact, but it depends on the requirements of your course.

I see... Thanks a lot! (Nod)
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K