Search code examples
graph-theoryshortest-pathminimum-spanning-tree

What is the complexity of the following code? Is it O(n^3log(n))?


What is the complexity of the following code? Is it O(n^3log(n))?

#G is an undirected dense graph, which has N vertices.
import networkx as nx
def cal_minimax_path_matrix(G):
    MST = nx.minimum_spanning_tree(G)
    minimax_matrix = np.zeros((N, N))

    for i in range(N):
        for j in range(N):
            if j > i:
                max_weight = -1 
                path = nx.shortest_path(MST, source=i, target=j)
                for k in range(len(path)-1):
                    if( MST.edges[path[k],path[k+1]]['weight'] > max_weight):
                        max_weight = MST.edges[path[k],path[k+1]]['weight']
                minimax_matrix[i,j] = minimax_matrix[j,i] = max_weight
                
    return minimax_matrix

The complexity of constructing a minimum_spanning_tree is O(n^2). What is the complexity of finding a shortest path in a minimum_spanning_tree? Is it O(nlog(n))?

The code is based on Madhav-99's code, see:

https://github.com/Madhav-99/Minimax-Distance


Solution

  • This is the code with comments added about each section's complexity in O:

    #G is an undirected dense graph, which has N vertices.
    import networkx as nx
    def cal_minimax_path_matrix(G):
        MST = nx.minimum_spanning_tree(G) # O(n²) (given)
        minimax_matrix = np.zeros((N, N)) # O(n²) (Initializes a NxN matrix, takes NxN time)
    
        for i in range(N): # O(n) (loop)
            for j in range(N): # O(n) (loops combined: O(n)*O(n)=O(n²))
                if j > i:
                    max_weight = -1 # O(1) (constant)
                    path = nx.shortest_path(MST, source=i, target=j) # Uses Dijkstra algorithm by default [1], which is O(ElogV) [2], applied to a minimum spanning tree this will be O(V-1 log V) [3] -> O((n-1) log n) -> O(n log n)
                    for k in range(len(path)-1): # O(n) as the path can be at most n long
                        if( MST.edges[path[k],path[k+1]]['weight'] > max_weight):
                            max_weight = MST.edges[path[k],path[k+1]]['weight']
                    minimax_matrix[i,j] = minimax_matrix[j,i] = max_weight
                    
        return minimax_matrix
    

    given the complexity of MST = nx.minimum_spanning_tree(G) is O(n²).

    Now adding up the complexity for the loops:

    O(n) (inner loop)
    + O(n log n) (`nx.shortest_path`)
    = O(n log n)
    * O(n²) (outer loops)
    = O(n³ log n)
    

    Now we have three complexities:

    • O(n²) for initializing the MST
    • O(n²) for initializing the matrix
    • O(n³ log n) for the loop

    As O(n³ log n) > O(n²) (read: O(n³ log n) dominates O(n²)), the overall complexity of cal_minimax_path_matrix(n) is O(n³ log n).

    References:
    [1] https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.generic.shortest_path.html
    [2] https://stackoverflow.com/a/26548129/8517948
    [3] https://math.stackexchange.com/questions/1524709/minimum-spanning-tree-edge-count