Thread: Floyd-Warshall algorithm on undirected graphs

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Floyd-Warshall algorithm on undirected graphs

    I am referring to the algorithm described in Wikipedia. Will the same following condition
    Code:
    if dist[i][j] > dist[i][k] + dist[k][j] 
                 dist[i][j] = dist[i][k] + dist[k][j]
    work for undirected graphs if I initialize the distance matrix using the rule
    Code:
    dist[i][j] = dist[j][i]
    .

    My opinion is that it should work since an undirected graph can also be represented as a directed graph using 2 opposite directed edges for every edge in the undirected graph.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Yes, it will.
    In fact, this algorithm is meant to be used for undirected graphs, in general.

    For directed graphs, which are often more sparse, Johnson's algorithm is generally better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floyd triangle - hint plz
    By dipen45 in forum C Programming
    Replies: 30
    Last Post: 11-20-2013, 01:56 PM
  2. Problem with Floyd's algorithm
    By Olar Adrian in forum C Programming
    Replies: 2
    Last Post: 05-14-2011, 09:43 AM
  3. Undirected graph and Binary Tree problem
    By arafat21 in forum C Programming
    Replies: 3
    Last Post: 05-02-2008, 05:52 AM
  4. Help with Floyd algorithm
    By diko in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-04-2005, 11:27 AM
  5. Minimize crossing edges in undirected graph
    By Shiro in forum C Programming
    Replies: 0
    Last Post: 12-26-2001, 04:48 AM