0
#include <bits/stdc++.h>

#define MAX_NODES 1005
#define INFINITE 
using namespace std;

vector<int> graph[MAX_NODES];
int numOfVertices, numOfEdges;

int shortest_path[MAX_NODES][MAX_NODES]; // shortest_path[i][j] holds the shortest path between i and j

int k;

int shortestPath(int i, int j) {
    // k ++;
    if (i == j) 
        shortest_path[i][j] = 1;

    // if we didn't solve shortest_path between i and j before

        // than solve it
    if (!shortest_path[i][j]) {

        int min_path = 10e6;
        for (auto vertice : graph[i])
            min_path = min(min_path, shortestPath(vertice, j) + 1);


        shortest_path[i][j] = min_path;
    }
    return shortest_path[i][j];
}

// the graph will be directed
void read() {
    int x, y; // temporary variables to read vertices and store them in our "graph"
    cin >> numOfVertices >> numOfEdges;

    for (int i = 0;i < numOfEdges;i ++) {
        cin >> x >> y;
        graph[x].push_back(y);
    }
}

void print() {
    for (int i = 0;i < numOfVertices;i ++) {
        if (graph[i].size())
            cout << i << " : ";
        for (int j = 0;j < graph[i].size();j ++) {
            cout << graph[i][j] << ' ';
        }
        if (graph[i].size())
            cout << '\n';
    }
}


int main() {
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);

    // ios_base :: sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);

    read();
    // print();

    int i = 1;
    int j = 7;
    int answer = shortestPath(i, j);

    if (answer == 10e6)
        printf("There are no paths between vertice %d and vertice %d\n", i, j);
    else
        printf("Shortest path between vertice %d and vertice %d ins: %d\n", i, j, answer - 1);

    // cout << k << endl;
    return 0;
}

The above program calculates the shortest path between 2 vertices in an unweighed DAG.

shortest_path[i][j] = shortest path between vertice i and vertice j.

What's the complexity of the function int shortestPath(int i, int j) ?

I think is O(V + E) where V is number of vertices and E number of edges but I don't know how to prove it.

Kalana
  • 4,683
  • 6
  • 22
  • 46
royer
  • 35
  • 1
  • 5

2 Answers2

0

If we don't count calls where the distance of the current node has already been computed, clearly there are at most V function calls. The complexity of a function call ignoring the recursive calls is linear with respect to the number of neighbors of the node, so the total complexity of these calls is O(E).

For the calls where the distance of the current node has already been computed, a constant number of these calls happen for each edge in the graph so the complexity is O(E). This gives a combined complexity of O(E).

Although the complexity of the function call is O(E), your shortest_path array has V^2 elements so creating the array alone is O(V^2).

BessieTheCookie
  • 4,571
  • 4
  • 13
  • 32
0

Notice that j is not changing in function call so the complexity of the function depends only on i.

shortest_path[i][j] = min_path is accomplishing two things: saving the minimum path from i to j and marking i visited (since j is not changing). Hence a vertex once visited will not be visited again.

So shortest_path will be called for the different values i can take let's call it N which is the total number of vertices in the graph. Therefore the minimum time complexity will be atleast O(N). Inside each call, the for loop runs for outdegree of vertex i. Now adding the for loop costs of all vertices, it will be outdegree of V1 + outdegree of V2 + ... + outdegree of Vn which equals O(E). Hence total time complexity is O(N + E)

srt1104
  • 929
  • 7
  • 10