Data Structures and Algorithms

Graph

Introduction to Graphs

Graphs are essential data structures used to represent relationships between entities, where vertices (or nodes) stand for the entities, and edges denote the connections between them. Whether you're modeling a social network where each person is a vertex and their friendships are edges, or mapping out cities and the roads between them, graphs offer a flexible way to capture complex relationships.

Types of Graphs

Undirected Graphs: In these graphs, edges do not have a direction, meaning the relationship is mutual. For instance, in a social network, if two users are friends, this relationship is bidirectional.
Directed Graphs: These graphs have edges with a specific direction, indicating one-way relationships. An example could be social media followings, where one user follows another without necessarily being followed back.
Weighted Graphs: Each edge in a weighted graph carries a weight, which could represent anything from distance to cost. This is particularly useful in scenarios like mapping out the shortest route in navigation systems.
Unweighted Graphs: Edges simply represent a connection between vertices without any associated weight or cost.
Cyclic Graphs: These graphs contain at least one cycle, a path that starts and ends at the same vertex, which is useful in scenarios like feedback loops in systems.
Acyclic Graphs: These graphs do not have any cycles, making them ideal for representing structures like hierarchical data or task dependencies, where loops are not allowed.
Connected Graphs: Every vertex in a connected graph can be reached from any other vertex, which is crucial in network design to ensure full connectivity.
Disconnected Graphs: In a disconnected graph, some vertices are isolated, meaning there is no path connecting them to others. This can be important for analyzing distinct components within a system.
Bipartite Graphs: Vertices in a bipartite graph can be divided into two distinct sets, with edges only running between vertices from different sets. This structure is often used in matching problems, like assigning tasks to employees.

Graph Representation

Adjacency Matrix: This method uses a 2D array to represent the graph, where rows and columns correspond to vertices, and the presence of an edge is indicated by a value (usually 1 or 0). While this is simple to visualize and quick for edge lookups, it can consume a lot of memory for large graphs with many vertices.
Adjacency List: More space-efficient than an adjacency matrix, this method uses lists to store the neighbors of each vertex. It’s particularly advantageous in sparse graphs where there are few edges, making it easier to traverse and manage.

Graph Traversal Techniques

Breadth-First Search (BFS): BFS explores the graph level by level, starting from a given source vertex and visiting all its immediate neighbors before moving on to the next level. This technique is particularly effective for finding the shortest path in an unweighted graph, making it useful in applications like peer-to-peer networking or network routing.
Depth-First Search (DFS): In contrast, DFS dives as deep as possible along a branch before backtracking, making it well-suited for problems where exploring all possible paths is necessary, such as in solving mazes or detecting cycles within a graph.
For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.