Overview
This class concluded the discussion on balanced binary search trees with Red-Black Trees and transitioned into Graph Theory. It reviewed heap operations and formally introduced graphs, covering fundamental definitions, terminology, and types such as undirected, directed, weighted, and complete graphs. The lecture also began comparing two primary graph representations: adjacency matrices and adjacency lists, setting the stage for graph traversal algorithms in subsequent classes.
Learning outcomes
- Define the properties of a Red-Black Tree and explain how they ensure balance.
- Differentiate between trees and graphs, and use correct graph terminology (vertices, edges, degree, paths).
- Classify graphs as undirected/directed, complete, weighted, or trees.
- Calculate in-degree, out-degree, and identify source and sink nodes in a digraph.
- Determine if a graph is strongly or unilaterally connected.
- Represent a graph using both an adjacency matrix and an adjacency list.
- Analyze the space efficiency of each representation based on graph density.
Core concepts
Red-Black Tree Balancing
A Red-Black Tree is a self-balancing binary search tree where each node has a color (red or black). The five color-based properties constrain tree height, ensuring no path is more than twice as long as any other, which guarantees O(log n) operations. This connects to prior BST discussions by offering an alternative balancing method to AVL trees.
Graphs as General Structures
A graph G = (V, E) is a set of vertices V connected by edges E. This generalizes trees, which are acyclic connected graphs. Understanding graphs is fundamental as they model networks, relationships, and paths, forming the basis for algorithms like search, shortest path, and spanning trees.
Graph Connectivity
Connectivity describes reachability between vertices. In directed graphs, strong connectivity requires mutual reachability for all pairs, while unilateral connectivity requires reachability in at least one direction. This concept is crucial for analyzing network robustness and information flow.
Graph Representation
The choice between an adjacency matrix (a 2D array) and an adjacency list (an array of linked lists) is a trade-off between space and time efficiency for different operations. This decision underpins the implementation and performance of all graph algorithms.
Formulas and maths
- Number of edges in a complete graph with
nvertices:\frac{n(n-1)}{2}
Symbols:n= number of vertices.
Use: Calculating the maximum number of edges in a simple, undirected graph. - Degree of each vertex in a complete graph:
n-1
Symbols:n= number of vertices.
Use: Determining the degree of any node in a fully connected undirected graph. - Number of edges in a tree with
mnodes:m-1
Symbols:m= number of nodes (vertices).
Use: Verifying if a connected graph is a tree based on its edge count. - Adjacency matrix entry:
A[i][j] = 1if edge exists fromitoj, else0.
Symbols:A= matrix,i= source vertex,j= destination vertex.
Use: Representing a graph sequentially to check for the existence of a specific edge in O(1) time. - Maximum number of keys in one node of an M-way search tree of order
M:M-1
Symbols:M= order of the tree (maximum number of children per node).
Use: For an order-4 (4-way) search tree, the maximum is4-1 = 3keys per node.
Worked examples
Heap Sort
Deleting the root repeatedly (each time performing heapify to restore the heap property) yields a sorted list. For a max-heap, this produces descending order; for a min-heap, ascending order.
RB Tree Property Check
A tree with a black root, red and black children was examined. Each path from the root to a leaf was verified to contain exactly 2 black nodes, satisfying property 5.
Graph Degree Calculation
For vertices V1 to V5 with edges: (V1,V2), (V1,V5), (V2,V3), (V2,V5), (V3,V4), (V4,V5).
Degrees: deg(V1)=2, deg(V2)=3, deg(V3)=2, deg(V4)=2, deg(V5)=3.
Complete Graph Edge Counts
- Triangle (3 vertices): Edges = 3×2/2 = 3.
- Square with diagonals (4 vertices): Edges = 4×3/2 = 6.
- Pentagon with all diagonals (5 vertices): Edges = 5×4/2 = 10.
Directed Graph In/Out-Degree
For a digraph with vertices V1 to V7 and directed edges, calculations yielded: - In-degrees: (0, 1, 2, 1, 1, 0, 2) - Out-degrees: (2, 1, 0, 1, 1, 2, 0) - Sources: V1, V6 (in-degree 0, out-degree > 0) - Sinks: V3, V7 (out-degree 0, in-degree > 0)
Adjacency Matrix Construction
For a graph with vertices A, B, C, D, E and directed edges, the 5x5 matrix was built:
A B C D E
A 0 1 0 0 0
B 0 0 1 0 0
C 1 0 0 1 0
D 0 0 0 0 1
E 1 0 1 1 0
In-degree of D = 1. Out-degree of D = 2.
Adjacency List Construction & Space Comparison
For the same graph, the adjacency list was: - A: B → C → E → null - B: C → null - C: D → null - D: E → null - E: C → null Space: Matrix uses 5x5 = 25 spaces. List uses 12 spaces (sum of list entries).
M-way Search Tree Key Count
For an order-4 (4-way) search tree, the maximum number of keys in a single node is M-1 = 4-1 = 3.
Questions asked in class
- Q: Why introduce Red-Black Trees when AVL trees already balance BST?
A: Both solve BST skewness. RB trees are "more stabilized" and maintaining color is simpler than calculating AVL balance factors. - Q: Are trees undirected graphs?
A: Trees are acyclic graphs; typically considered undirected/bi-directional, but directed trees exist (e.g., root to leaf). Usually, tree edges imply bi-directional movement. - Q: Can a tree have directed edges?
A: Yes, a directed tree can exist, but typically trees are treated as undirected/cyclic graphs. Some books refer to undirected graphs as bi-directional. - Q (Mubala): If the edge between B and C is reversed, would the graph be fully connected?
A: No. "Fully connected" here means strongly connected, which requires two-way reachability for all pairs, not just a direct edge between one pair. - Q (Dukesh): In the adjacency list example, does the sequence A->B->C->E represent a path?
A: No. The adjacency list only shows directly connected (adjacent) vertices for each node individually, not a continuous path. - Q: In a 4-way search tree, what is the maximum number of keys in one node?
A: 3, sinceM-1 = 4-1. - Q: What is the maximum number of keys in a single node of an order-4 M-way search tree?
A: 3.
Key takeaways
- A Red-Black Tree uses color (red/black) and a fixed black-height on all root-to-leaf paths to maintain balance, ensuring O(log n) operations.
- A graph G = (V, E) consists of vertices and edges, generalizing trees.
- In an undirected graph, degree is the count of incident edges; an isolated node has degree 0.
- A complete graph with n vertices has n(n-1)/2 edges, and each vertex has degree n-1.
- A tree with m nodes has exactly m-1 edges and no cycles.
- In a directed graph (digraph), edges are ordered pairs (u,v); in-degree counts incoming edges, out-degree counts outgoing edges.
- A source node has in-degree 0; a sink node has out-degree 0.
- A graph is strongly connected if every pair of vertices is mutually reachable; it is unilaterally connected if for every pair, reachability exists in at least one direction.
- An adjacency matrix uses O(n²) space but allows O(1) edge lookup.
- An adjacency list uses O(V + E) space, making it more efficient for sparse graphs.
- The adjacency list for a vertex stores only its adjacent (directly connected) vertices, not a path.
- The choice between adjacency matrix and list depends on graph density and the required operations.
- A node in an M-way search tree of order M holds at most M-1 keys; an order-4 tree holds at most 3.
Exam-style questions
- Given a directed graph's adjacency list, identify all source and sink nodes.
Outline: Calculate in-degree and out-degree for each vertex by traversing the list. Sources have in-degree 0 and out-degree > 0; sinks have out-degree 0 and in-degree > 0. - For a complete graph with 8 vertices, calculate the total number of edges and the degree of each vertex.
Outline: Apply formula: edges = n(n-1)/2 = 28. Each vertex degree = n-1 = 7. - Explain why an adjacency matrix might be preferred over an adjacency list for a dense graph, and vice versa for a sparse graph.
Outline: Matrix offers O(1) edge queries, beneficial when many edges exist (dense). List saves significant space when edges are few (sparse), as it stores only existing connections. - A Red-Black Tree path from root to leaf contains 3 black nodes. What is the maximum possible number of red nodes on this path?
Outline: Use property: red nodes cannot have red children. The maximum alternation is Black-Red-Black-Red-Black-Red-Black, giving 3 red nodes. - Determine if a described directed graph is strongly connected, unilaterally connected, or neither.
Outline: Check reachability for all vertex pairs. If all pairs are mutually reachable, it's strongly connected. If for every pair, reachability exists in at least one direction, it's unilaterally connected. Otherwise, it's neither. - What is the maximum number of keys in a single node of an order-4 M-way search tree?
Outline: ApplyM-1withM = 4, giving 3 keys per node.
Glossary
- Adjacency - In a directed graph, vertex v is adjacent to u if there is a direct edge from u to v.
- Adjacency List - A graph representation using an array of lists, where each list stores vertices adjacent to a given vertex.
- Adjacency Matrix - A square matrix used to represent a graph, where entry (i,j) indicates an edge from vertex i to j.
- Black-height - The number of black nodes on any path from a node to a descendant leaf (excluding the node itself).
- Complete Graph - An undirected graph where every pair of distinct vertices is connected by a unique edge.
- Degree - The number of edges incident to a vertex (undirected graph).
- Directed Graph (Digraph) - A graph where edges have direction, represented as ordered pairs (u, v).
- Heapify - The operation to restore the heap property after the root is removed or a key is changed.
- In-degree - The number of directed edges entering a vertex.
- M-way Search Tree - A search tree in which each node has at most
Mchildren and at mostM-1keys. - Out-degree - The number of directed edges leaving a vertex.
- Path - A sequence of vertices where each consecutive pair is connected by an edge.
- Red-Black Tree - A balanced binary search tree with colored nodes (red/black) satisfying five specific properties.
- Sink - A vertex in a directed graph with out-degree 0.
- Source - A vertex in a directed graph with in-degree 0.
- Strongly Connected Graph - A directed graph where every vertex is reachable from every other vertex.
- Unilaterally Connected Graph - A directed graph where for any pair u, v, there is a path from u to v or from v to u.
Follow-ups
- Insertion and deletion algorithms for Red-Black Trees were described as complex and were not covered in detail; related PPT slides will be shared.
- Detailed graph algorithms, including Depth-First Search (DFS) and Breadth-First Search (BFS), are scheduled for the next class.
- The Minimum Spanning Tree problem and Kruskal-Prim algorithm were mentioned as upcoming topics.
- Other graph operations like search, insertion, and deletion will also be covered in future lectures.
Quiz cross-check (added 2026-12-01, from DAA Quiz 2)
- The adjacency matrix of an undirected graph is symmetric: the edge {i, j} is stored in both directions, so A[i][j] = A[j][i] and the matrix mirrors about the main diagonal. The 5×5 matrix built in this class was directed, which is why it was not symmetric. An entry of 1 means "an edge exists from i to j"; visited status and vertex degree are not stored in the matrix — visited is a separate array owned by the traversal, and degree is read off the row/column counts.