Timer is Running

Given a graph of **V** nodes represented in the form of the adjacency matrix. The task is to find the shortest distance of all the vertex's from the source vertex.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer V denoting the size of the adjacency matrix and in the next line are V*V space-separated values, which denote the weight of an edge of the matrix (gr[i][j] represents the weight of an edge from ith node to jth node). The third line of each test case contains an integer denoting the source vertex s.

**Output:**

For each test, case output will be V space-separated integers where the ith integer denotes the shortest distance of the ith vertex from source vertex.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **dijkstra()** which takes the adjacency matrix of the Graph **g**, the source vertex **src **and the number of vertices **V **as inputs and returns a list containing the minimum distance of all the vertices from the source vertex.

**Expected Time Complexity:** O(V^{2}).

**Expected Auxiliary Space:** O(V).

**Constraints:**

1 <= T <= 20

1 <= V <= 100

0 <= graph[i][j] <= 1000

0 <= s < V

**Example:**

**Input**:

2

2

0 25 25 0

0

3

0 1 43 1 0 6 43 6 0

2

**Output**:

0 25

7 6 0

**Explanation:
Testcase 1: **Shortest distance of source node 0 to 1 is 25, and shortest distance of source to source is 0.

You must assume that graph[i][j] = 0 means that the path from i to j does not exist.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

lone_walker | 398 |

2isprime | 337 |

Umakant_Dubey | 314 |

rathindran | 262 |

saiujwal13083 | 251 |

saiujwal13083 | 889 |

ppsvikhyat17 | 712 |

lone_walker | 684 |

generic_coder | 630 |

NaveenGarg2 | 554 |

blackshadows | 5448 |

Ibrahim Nash | 5438 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.