Graph - 7: Check if Undirected Graph is Connected

preview_player
Показать описание
Solution:
- We'll achieve this via DFS approach.
- Take stack & boolean array
- We'll put any starting index in stack & mark true for this index
- Now we'll poll top element from stack & fetch list for index. This'll show all neighbours connected to this index
- Now we'll add all elements which are not visited & insert into stack
- Do above until stack is not empty
- If all values in boolean array is True, then graph is connected, else it's disconnected

Time Complexity: O(V + E)

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

You are doing great sir,
Thanks for helping fresh programmers like me.

sachindobal
Автор

Hi, why can't we use for loop to see if any row or column is empty, so we can confirm whether it's connected or not?

vishnunalubola
Автор

we can also get it from Adjacency List like below, Instead of traversing and visiting nodes?

for(let i in this.adjacencyList){
== 0){
return 'Not Connected';
}
}

return 'Connected';

sushobithapsmusic