Javatpoint Logo
Javatpoint Logo

Islands in a graph using BFS


Graphs are basic data structures that show an arrangement of edges and nodes. They are used in many different domains, such as computer networks and social networks. Finding islands within a graph is an intriguing topic in graph theory. When discussing graphs, islands are frequently referred to as connected components or subgraphs in which every node can be reached from every other node.

A graph traversal algorithm called Breadth-First Search investigates every vertex in a graph level by level. Before going on to the next level of vertices, BFS investigates its neighbors starting from a specified source vertex. Because of this feature, BFS is appropriate for tasks such as determining the shortest path between two nodes or identifying linked components within a network. Imagine a situation where a network of connected nodes is represented as a graph. In this context, an island is a collection of nodes that are linked to one another but not to nodes outside the group. In such a graph, we may use BFS to explore and mark related components in order to determine the number of islands.

Islands in a graph using BFS



Islands in a graph using BFS

Code Explanation

Graph Illustration

  • The graph is represented as an adjacency list in the code. The Graph structure comprises an array of linked lists (adjacencyList) and the number of vertices (numVertices). Each element of the array represents a vertex, and the linked list that corresponds to it records the vertices that are adjacent to it.

Node Creation

  • The Node structure represents a node in the linked list. The task of establishing a new node with a specified data value falls to the createNode function.

Graph Development

  • A new graph is initialized with a predetermined number of vertices using the createGraph function. The graph structure's memory is allocated, and NULL pointers are used to initialize the adjacency list.

Edge Addition

  • An undirected edge is added to the graph between two vertices via the addEdge function. It updates the adjacency lists by generating new nodes at the source and destination vertices.

Breadth-First Search (BFS)

  • The BFS function starts at a specified vertex (startVertex) and traverses the BFS. It searches neighboring vertices level by level and keeps track of visited vertices using a queue. The First In First Out (FIFO) principle directs the traversal.

Main Function

  • A graph with five vertices is constructed in the main function, and edges are added to show the connections between the vertices. To keep track of whether a vertex has been visited during BFS, utilize the visited array.

Identification of the Island

  • Subsequently, the primary function traverses all vertices and executes BFS on every unexplored vertex. The vertices are printed for each connected component (island) that is found in the order that they are visited during BFS.

Time and Space Complexity

The Breadth-First Search (BFS) algorithm affects the time complexity of the given C code. In the worst-case scenario, when all vertices and edges are explored, the time complexity is O(V + E), where V is the number of vertices and E is the number of edges. This is because BFS traverses each vertex and edge once. In the code's specific example, where the number of vertices is denoted by V and the number of edges is denoted by E, the time complexity can be regarded as O(V + E).

The data structures that are utilized to implement BFS and describe the graph largely dictate the space complexity. O(V + E) space is needed for the graph's adjacency list representation, where V denotes the number of vertices and E is the number of edges. The BFS queue, which, in the worst-case scenario, might have a maximum size of V, is the source of the extra space complexity. Because of the space needed for the BFS traversal and the graph representation, the code's overall space complexity is O(V + E).

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA