Core Graph Concepts and Terminology
Understanding graph terminology is the foundation for mastering this topic. A vertex (or node) is a fundamental unit in a graph. An edge is a connection between two vertices.
Classifying Graphs
Graphs have distinct types based on their structure. Directed graphs (digraphs) have edges with direction, while undirected graphs have bidirectional edges. Weighted graphs assign numerical values to edges representing costs or distances. Acyclic graphs have no cycles, which matters for algorithms like topological sorting.
Key Properties to Know
The degree of a vertex counts how many edges connect to it. A path is a sequence of vertices connected by edges. A cycle occurs when you start at a vertex and return to it following edges. Understanding these distinctions is critical because they determine which algorithms work.
For example, topological sorting only works on directed acyclic graphs. Dijkstra's algorithm requires non-negative weights. Flashcards excel here by helping you recall definitions instantly. Create cards showing a term on one side and a concise definition with a brief example on the other. Practice until you can instantly recall that a tree is an acyclic connected graph with exactly n-1 edges for n vertices.
Graph Representation Methods
How you represent a graph in code affects algorithm efficiency and implementation complexity. The two primary representations are adjacency matrices and adjacency lists.
Adjacency Matrices
An adjacency matrix is a 2D array where matrix[i][j] represents whether an edge exists between vertex i and vertex j. This offers O(1) lookup time for checking if an edge exists. However, it requires O(v squared) space regardless of how many edges exist. This makes it inefficient for sparse graphs with few edges.
Adjacency Lists
Adjacency lists store a list of neighbors for each vertex using an array of linked lists or hash maps. They require O(v + e) space, making them ideal for sparse graphs. For dense graphs with many edges, adjacency matrices may be preferable instead.
Other Representations
Edge lists store pairs of connected vertices, useful for algorithms like Kruskal's for finding minimum spanning trees. Flashcards help you visualize these tradeoffs by showing a representation on one side and asking which performs better for specific scenarios. Make cards comparing space and time complexity for each representation. Create visual cards showing the same graph rendered as both a matrix and an adjacency list.
Traversal Algorithms: DFS and BFS
Graph traversal is the foundation for solving most graph problems. Depth-First Search (DFS) and Breadth-First Search (BFS) are the two core algorithms you must master.
Understanding DFS
DFS explores as far as possible along each branch before backtracking. It uses either recursion or an explicit stack. DFS visits vertices in the order it discovers them going deep into branches. It works well for topological sorting, cycle detection, strongly connected components, and finding paths in mazes. DFS uses O(h) space where h is height.
Understanding BFS
BFS explores vertices level by level using a queue. It visits all neighbors of a vertex before moving deeper. BFS is optimal for finding shortest paths in unweighted graphs, level-order traversal, and checking connectivity. BFS uses O(w) space where w is maximum width.
Time and Space Tradeoffs
Both algorithms have O(v + e) time complexity but differ in space usage and traversal order. Understanding when to apply each is crucial. Flashcards are invaluable because you need to memorize the algorithmic steps and recognize when to use each approach. Create step-by-step process cards showing DFS and BFS pseudocode. Add cards showing example graphs with traversal order marked. Include scenario cards asking which algorithm solves a given problem.
Advanced Algorithms: Shortest Path and Minimum Spanning Trees
Once you master traversal, advanced graph algorithms become accessible. These algorithms solve specific optimization problems on weighted graphs.
Shortest Path Algorithms
Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in weighted graphs with non-negative weights. It uses a greedy approach with a priority queue. Time complexity is O((v + e) log v) with a binary heap.
The Bellman-Ford algorithm finds shortest paths even with negative weights but cannot handle negative cycles. It is O(ve) and less efficient than Dijkstra's for non-negative weights.
Floyd-Warshall computes shortest paths between all pairs of vertices in O(v cubed) time. Use it when you need all-pairs shortest paths.
Minimum Spanning Tree Algorithms
Kruskal's algorithm sorts edges by weight and adds them if they do not create a cycle. It uses Union-Find for efficiency. Prim's algorithm grows the tree by repeatedly adding the minimum-weight edge connecting the tree to a new vertex. Both algorithms guarantee the same total weight but may produce different trees.
Mastering Through Flashcards
Flashcards help you memorize which algorithm to use for different scenarios and the steps of each algorithm. Create cards with algorithm pseudocode and problem-type cards asking which algorithm applies. Include cards comparing space-time tradeoffs between approaches.
Problem-Solving Patterns and Study Strategies
Graph problems follow recognizable patterns. Identifying the pattern helps you select the right algorithm quickly.
Common Problem Types
Connectivity problems ask whether vertices are reachable from one another. Solve these with DFS or BFS. Path problems ask for routes between vertices. Use shortest-path algorithms or traversals. Cycle detection uses DFS with a recursion stack to detect back edges. Topological sorting applies to DAGs to order vertices such that all edges point forward. This is essential for dependency resolution.
Bipartite checking determines if vertices can be divided into two groups with edges only between groups. Strong connectivity and component finding require analyzing directed graphs carefully.
Effective Study Techniques
For effective studying, use the Feynman Technique with flashcards. After studying a concept, write it in simple terms on a flashcard. Test whether you can explain it without the card. Create pattern-recognition cards showing problem descriptions and asking which algorithm family applies. Practice categorizing mixed problems by type before solving them.
Review cards for common mistakes and edge cases, like forgetting to handle disconnected components. Space your review strategically using spaced repetition. Review cards more frequently when first learning and less frequently as you master them. This approach moves information from short-term to long-term memory.
