Graph theory and algorithms form the backbone of many computational problems. This unit covers fundamental concepts like graph representations, traversal methods, and key algorithms for solving common graph-related challenges.
Advanced topics include network flow, matching problems, and optimization techniques. Real-world applications span social networks, transportation systems, and biological interactions, showcasing the versatility of graph-based approaches in problem-solving.
Key Concepts and Terminology
Graphs consist of vertices (nodes) connected by edges (links) representing relationships or connections between entities
Directed graphs (digraphs) contain edges with a specific direction, while undirected graphs have edges without a direction
Weighted graphs assign values (weights) to edges, representing costs, distances, or capacities
Degree of a vertex refers to the number of edges incident to it, with in-degree and out-degree for directed graphs
Paths are sequences of vertices connected by edges, with simple paths having no repeated vertices
Cycles are paths that start and end at the same vertex
Connected graphs have a path between every pair of vertices
Trees are connected acyclic graphs, while forests are disjoint sets of trees
Bipartite graphs divide vertices into two disjoint sets, with edges only between sets
Planar graphs can be drawn on a plane without edge crossings, while non-planar graphs require edge crossings
Graph Representations and Properties
Adjacency matrix represents a graph using a square matrix, with entries indicating edge presence or weight
Space complexity of O(V2), suitable for dense graphs
Edge existence and weight lookup in constant time O(1)
Adjacency list stores a graph as an array of lists, with each list containing the neighbors of a vertex
Space complexity of O(V+E), efficient for sparse graphs
Allows for efficient traversal of a vertex's neighbors
Edge list is a simple representation storing a list of edges, each represented as a pair of vertices
Incidence matrix represents the relationship between vertices and edges, with rows for vertices and columns for edges
Graph density measures the ratio of actual edges to the maximum possible edges, with sparse and dense graphs
Connectivity properties include connected components, strongly connected components (SCCs) in directed graphs, and bridges/articulation points
Graph isomorphism determines if two graphs are structurally equivalent, with efficient algorithms for special cases
Basic Graph Algorithms
Breadth-First Search (BFS) traverses a graph level by level, exploring all neighbors before moving to the next level
Finds shortest paths in unweighted graphs and connected components
Time complexity of O(V+E) using a queue data structure
Depth-First Search (DFS) explores a graph by going as deep as possible before backtracking
Useful for cycle detection, topological sorting, and finding strongly connected components
Implemented using a stack or recursion, with time complexity O(V+E)
Topological sorting orders the vertices of a directed acyclic graph (DAG) such that for every directed edge (u,v), u comes before v
Kahn's algorithm and DFS-based approaches are common
Minimum Spanning Tree (MST) algorithms (Kruskal's, Prim's) find a tree that connects all vertices with minimum total edge weight
Kruskal's algorithm sorts edges by weight and adds them greedily, avoiding cycles
Prim's algorithm grows the MST from a starting vertex, adding the cheapest connected edge at each step
Shortest path algorithms (Dijkstra's, Bellman-Ford) find paths with minimum total weight between vertices
Dijkstra's algorithm uses a priority queue and greedy approach for non-negative weights
Bellman-Ford handles negative weights and detects negative cycles
Advanced Graph Algorithms
Floyd-Warshall algorithm finds shortest paths between all pairs of vertices using dynamic programming
Computes a distance matrix in O(V3) time, handling negative weights but not negative cycles
Johnson's algorithm combines Bellman-Ford and Dijkstra's algorithms for sparse graphs with negative weights
Reweights edges to eliminate negative weights, then runs Dijkstra's from each vertex
Maximum flow algorithms (Ford-Fulkerson, Edmonds-Karp) find the maximum flow through a network from source to sink
Ford-Fulkerson uses augmenting paths to increase flow, with Edmonds-Karp using BFS for better performance
Minimum cut algorithms (Karger's, Stoer-Wagner) find a cut with minimum total weight that separates a graph into two parts