Find the greatest of the shortest paths

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

Discussion Overview

The discussion revolves around the algorithm for finding the diameter of a graph, defined as the greatest of the shortest paths. Participants explore the implementation of the Floyd-Warshall algorithm to compute this diameter, addressing both the algorithm's correctness and its time complexity.

Discussion Character

  • Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • Some participants propose using the Floyd-Warshall algorithm to compute the diameter of the graph by first calculating the shortest-path weights.
  • Others question the assumption that the algorithm correctly computes the graph diameter, asking for clarification on the reasoning behind this claim.
  • There are suggestions that a word description of the algorithm may be more accessible than code for understanding its function.
  • Participants discuss the time complexity of the proposed algorithm, with some asserting it is $\Theta(n^3)$ due to the nature of the Floyd-Warshall algorithm and the subsequent operations.
  • One participant challenges the classification of the time complexity of the outer loop, arguing that it should be considered as $O(n^2)$ when accounting for the inner loop's execution.
  • Another participant acknowledges that the time complexity of the Floyd-Warshall algorithm is a well-known fact but notes that its inclusion may depend on course requirements.

Areas of Agreement / Disagreement

Participants express differing views on the clarity and correctness of the algorithm presented, with some agreeing on the need for a clearer explanation while others maintain that the code suffices. The discussion on time complexity also reveals differing interpretations, indicating unresolved disagreements on the classification of loop complexities.

Contextual Notes

There is an ongoing debate about the clarity of the algorithm's presentation and the assumptions made regarding its correctness. The discussion also highlights the importance of understanding time complexity in the context of algorithm analysis.

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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K