![]() |
VOOZH | about |
A graph is a non-linear data structure that consists of a set of nodes (also known as vertices) connected by edges. Unlike trees, which have a hierarchical structure, graphs can represent more complex relationships, such as social networks, web pages, road maps, and more.
Graphs can be classified based on various characteristics:
A cycle in a graph is a path that starts and ends at the same vertex, with all edges in the cycle being distinct. In directed graphs, cycles are particularly significant, as they can affect the traversal or computation of graph algorithms.
An adjacency matrix is a 2D array used to represent a graph. For a graph with n vertices, the matrix is of size n x n, where each element at position [i][j] indicates the presence (or weight) of an edge between vertex i and vertex j.
An adjacency list is a collection of lists or arrays where each list corresponds to a vertex in the graph and contains the nodes that are directly connected to it by an edge. This representation is more space-efficient than an adjacency matrix, especially for sparse graphs.
The time complexity for accessing an edge in an adjacency matrix is O(1), as you can directly access the element at position [i][j] to check if there is an edge between vertex i and vertex j.
The time complexity for accessing an edge in an adjacency list is O(deg(v)), where deg(v) is the degree of vertex v (the number of edges connected to it). This is because you need to iterate through the list of adjacent nodes to check for a specific edge.
Depth-first search (DFS) is an algorithm used for traversing or searching a graph. It starts at the root (or an arbitrary node in the case of an undirected graph) and explores as far as possible along each branch before backtracking. DFS is implemented using either a recursive approach or a stack.
Breadth-first search (BFS) is an algorithm used to traverse or search a graph in a level-order manner. It starts at the root and explores all its neighbors before moving on to the next level of neighbors. BFS is implemented using a queue.
The time complexity of both depth-first search (DFS) and breadth-first search (BFS) is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is because both algorithms visit every vertex and edge once.
The shortest path in an unweighted graph can be found using breadth-first search (BFS). BFS guarantees the shortest path because it explores nodes level by level, ensuring that when a node is first visited, it is visited via the shortest possible path.
To find the shortest path in a weighted graph, Dijkstra's algorithm is commonly used. It maintains a set of nodes whose shortest distance from the source is known and iteratively explores the nearest unvisited node, updating the shortest distances of its neighbors.
Topological sorting is the process of ordering the vertices of a directed acyclic graph (DAG) such that for every directed edge u -> v, vertex u comes before v in the ordering. Topological sorting is used in tasks such as job scheduling and dependency resolution.
Dijkstra's algorithm is used to find the shortest path in weighted graphs with non-negative edges. It initializes distances to infinity (except the source, which is 0), uses a priority queue to process nodes with the smallest tentative distance, and updates neighbors if a shorter path is found. The process repeats until all nodes are processed, ensuring the shortest path.
A spanning tree of a graph is a subgraph that includes all the vertices of the graph and is a tree (i.e., it has no cycles). A graph can have multiple spanning trees, and finding a minimum spanning tree (MST) is a common problem in graph theory.
Kruskal’s algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a graph. It works by sorting all edges in increasing order of their weights and adding edges to the MST while ensuring no cycles are formed.
Prim’s algorithm is another greedy algorithm used to find the minimum spanning tree (MST) of a graph. It starts with an arbitrary vertex and grows the MST by adding the nearest vertex that is not already in the tree, selecting the edge with the minimum weight.
Advantages:
Disadvantages:
To detect a cycle in a directed graph, you can use depth-first search (DFS) with an auxiliary array to track visited nodes. During DFS, if a node is revisited while still in the recursion stack (i.e., it is currently being explored), a cycle is detected.
The following list of 50 coding problems on Graphs covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.