Mohammad Shahhoud
Mohammad Shahhoud

Reputation: 479

Time Complexity O(V^3) or O(V^2)?

I'm new for analyzing the algorithms and the time for them.. This algorithm is posted in http://geeksforgeeks.com and they wrote that the time complexity of the algorithm is O(V^2) which i think that it's O(V^3):

    int minDistance(int dist[], bool sptSet[])
{
   // Initialize min value
   int min = INT_MAX, min_index;

   for (int v = 0; v < V; v++)
     if (sptSet[v] == false && dist[v] <= min)
         min = dist[v], min_index = v;

   return min_index;
}

// A utility function to print the constructed distance array
int printSolution(int dist[], int n)
{
   printf("Vertex   Distance from Source\n");
   for (int i = 0; i < V; i++)
      printf("%d \t\t %d\n", i, dist[i]);
}

// Funtion that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
     int dist[V];     // The output array.  dist[i] will hold the shortest
                      // distance from src to i

     bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
                     // path tree or shortest distance from src to i is finalized

     // Initialize all distances as INFINITE and stpSet[] as false
     for (int i = 0; i < V; i++)
        dist[i] = INT_MAX, sptSet[i] = false;

     // Distance of source vertex from itself is always 0
     dist[src] = 0;

     // Find shortest path for all vertices
     for (int count = 0; count < V-1; count++)
     {
       // Pick the minimum distance vertex from the set of vertices not
       // yet processed. u is always equal to src in first iteration.
       int u = minDistance(dist, sptSet);

       // Mark the picked vertex as processed
       sptSet[u] = true;

       // Update dist value of the adjacent vertices of the picked vertex.
       for (int v = 0; v < V; v++)

         // Update dist[v] only if is not in sptSet, there is an edge from 
         // u to v, and total weight of path from src to  v through u is 
         // smaller than current value of dist[v]
         if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX 
                                       && dist[u]+graph[u][v] < dist[v])
            dist[v] = dist[u] + graph[u][v];
     }

     // print the constructed distance array
     printSolution(dist, V);
}

Where the graph represented inside graph[][] (matrix representation).

Thanks in advance

Upvotes: 0

Views: 357

Answers (1)

amit
amit

Reputation: 178491

The solution is indeed O(V^2):

 for (int i = 0; i < V; i++)
    dist[i] = INT_MAX, sptSet[i] = false;

This part runs BEFORE the main loop, and in complexity of O(V) -.

 for (int count = 0; count < V-1; count++)
 {

This is the main loop, it runs O(V) times overall, and each time it requires:

   int u = minDistance(dist, sptSet);

This runs one time per each different value of count, and its complexity is O(V), so we have O(V^2)` by now.

  sptSet[u] = true;

This is O(1), and runs O(V) times.

   for (int v = 0; v < V; v++)

This loop runs O(V) times, for each value of count, let's examine what happens each time you run it:

    if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX 
                                   && dist[u]+graph[u][v] < dist[v])
        dist[v] = dist[u] + graph[u][v];

All of those are O(1), and done per each (count,v) pair, and there are O(V^2) of those pairs.

So, totally O(V^2).


Note that for more efficient graph representation, we can run Dijkstra's algorithm in O(E + VlogV), which might be better in case of sparse graphs.

Upvotes: 2

Related Questions