Quiz 2
Registry Synced

Graph Fundamentals

1049 words
5 min read

Reading compass

Now · 🎯 Learning Objectives

Graph Fundamentals

🎯 Learning Objectives

By the end of this topic, you will be able to:
  1. Define a graph and its components (vertices, edges)
  2. Distinguish between directed and undirected graphs
  3. Represent graphs using adjacency matrices and adjacency lists
  4. Compute the degree (in-degree/out-degree) of vertices
  5. Identify special graphs: complete, simple, bipartite

📋 Prerequisites

  • Sets — sets of vertices and edges
  • Basic matrix multiplication for adjacency matrix operations

📖 Core Content

18.1 Intuition: Modeling Relationships

A graph is a mathematical structure for modeling pairwise relationships. Think of a social network: people are vertices (nodes), friendships are edges (connections). Graphs are everywhere:
  • Social networks (Facebook friends)
  • Road maps (cities connected by roads)
  • The internet (web pages linked by URLs)
  • Dependency management (packages depend on other packages)
💡 Why this matters: Graph algorithms power Google Maps (shortest path), Facebook's friend suggestions, package managers (dependency resolution), and recommendation systems.

18.2 Formal Definition

A graph G=(V,E)G = (V, E) consists of:
  • VV: a set of vertices (nodes)
  • EE: a set of edges (pairs of vertices)

18.2.1 Types of Graphs

(Diagram)
TypeDescriptionEdge Notation
UndirectedEdges have no direction(u,v)=(v,u)(u,v) = (v,u)
DirectedEdges have a directionuvu \to v
SimpleNo loops, no multiple edgesStandard
Complete KnK_nEvery pair of vertices connectedn(n1)/2n(n-1)/2 edges
BipartiteVertices split into 2 sets, edges only between sets

18.3 Graph Representations

18.3.1 Adjacency Matrix

A V×V|V| \times |V| matrix AA where:
A[i][j]={1if edge (i,j)E0otherwiseA[i][j] = \begin{cases} 1 & \text{if edge } (i, j) \in E \\ 0 & \text{otherwise} \end{cases}
For undirected graphs: AA is symmetric. For weighted graphs: A[i][j]=A[i][j] = weight (not just 0/1). Example: Graph with vertices {1,2,3}\{1,2,3\} and edges {(1,2),(2,3),(3,1)}\{(1,2), (2,3), (3,1)\}
A=[011101110]A = \begin{bmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 0 \end{bmatrix}

18.3.2 Adjacency List

For each vertex, store a list of its neighbors. Example (same graph):
pseudo
1 → [2, 3]
2 → [1, 3]
3 → [1, 2]
RepresentationMemoryEdge LookupNeighbor Iteration
MatrixO(V2)O(V^2)O(1)O(1)O(V)O(V)
ListO(V+E)O(V + E)O(degree)O(degree)O(degree)O(degree)

18.4 Degree of Vertices

DefinitionNotation
Degree (undirected)Number of edges incident to vertexdeg(v)\deg(v)
In-degree (directed)Number of edges entering vvdeg(v)\deg^-(v) or in(v)\text{in}(v)
Out-degree (directed)Number of edges leaving vvdeg+(v)\deg^+(v) or out(v)\text{out}(v)
Handshaking Lemma: In any undirected graph, vVdeg(v)=2E\sum_{v \in V} \deg(v) = 2|E|. In a directed graph, deg(v)=deg+(v)=E\sum \deg^-(v) = \sum \deg^+(v) = |E|.

18.5 Special Graphs

GraphDefinition#EdgesExample
Complete KnK_nAll n(n1)/2n(n-1)/2 possible edgesn(n1)/2n(n-1)/2K5K_5 has 10 edges
Cycle CnC_nnn vertices in a cyclennC4C_4 is a square
Path PnP_nnn vertices in a linen1n-1
Bipartite Km,nK_{m,n}Complete bipartitemnm \cdot nK3,3K_{3,3}

18.6 Worked Examples

Example 1: Draw the graph G=(V,E)G = (V, E) with V={1,2,3,4}V = \{1,2,3,4\} and E={(1,2),(2,3),(3,4),(4,1),(1,3)}E = \{(1,2), (2,3), (3,4), (4,1), (1,3)\}. Write its adjacency matrix.
A=[0111101011011010]A = \begin{bmatrix} 0 & 1 & 1 & 1 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \end{bmatrix}
Example 2: For a directed graph with edges 121 \to 2, 232 \to 3, 313 \to 1, 131 \to 3, find in-degrees and out-degrees.
VertexIn-degreeOut-degree
11 (from 3)2 (to 2, 3)
21 (from 1)1 (to 3)
32 (from 1, 2)1 (to 1)
Verify: in=out=4=E\sum \text{in} = \sum \text{out} = 4 = |E|

📐 Key Formulas — Summary Table

ConceptFormulaNotes
Complete graph edgesn(n1)/2n(n-1)/2KnK_n
Bipartite edgesmnm \cdot nKm,nK_{m,n}
Handshaking (undirected)$\sum \deg(v) = 2E
Handshaking (directed)$\sum \text{in}(v) = \sum \text{out}(v) =E
Adjacency matrix sizeV×VV \times V
Adjacency list memoryO(V+E)O(V+E)

⚠️ Common Pitfalls

Pitfall 1: Confusing Directed and Undirected Edge Notation

In directed: (u,v)(u,v) means uvu \to v, not vuv \to u. In undirected: (u,v)=(v,u)(u,v) = (v,u).

Pitfall 2: Forgetting Symmetry in Undirected Adjacency Matrix

Undirected graphs have symmetric adjacency matrices: A[i][j]=A[j][i]A[i][j] = A[j][i].

Pitfall 3: Degree Counting

In directed graphs, the total degree concept doesn't apply directly — we use in-degree and out-degree separately.

📝 Practice Questions

Q1: How many edges does K6K_6 have?
6(5)/2=156(5)/2 = 15
15\boxed{15} Q2: Draw the adjacency matrix for a directed graph with 121 \to 2, 232 \to 3, 313 \to 1.
>A=[010001100]>> A = \begin{bmatrix}0&1&0\\0&0&1\\1&0&0\end{bmatrix} >
>[010001100]>> \boxed{\begin{bmatrix}0&1&0\\0&0&1\\1&0&0\end{bmatrix}} >
Q3: In an undirected graph with sum of degrees = 24, how many edges?
2E=24    E=122|E| = 24 \implies |E| = 12
12\boxed{12} Q4: List the adjacency list for K3K_3.
1[2,3]1 \to [2,3], 2[1,3]2 \to [1,3], 3[1,2]3 \to [1,2]
1:[2,3],2:[1,3],3:[1,2]\boxed{1:[2,3], 2:[1,3], 3:[1,2]} Q5: Can a simple graph have a vertex of degree 0?
Yes — an isolated vertex.
Yes\boxed{\text{Yes}} Q6: What is the sum of in-degrees in any directed graph with 10 edges?
1010
10\boxed{10} Q7: Draw C4C_4 and give its adjacency matrix.
>A=[0101101001011010]>> A = \begin{bmatrix}0&1&0&1\\1&0&1&0\\0&1&0&1\\1&0&1&0\end{bmatrix} >
Cycle graph with 4 vertices\boxed{\text{Cycle graph with 4 vertices}} Q8: How many vertices does K3,4K_{3,4} have? How many edges?
Vertices: 3+4=73+4 = 7, Edges: 34=123\cdot4 = 12
7 vertices, 12 edges\boxed{7 \text{ vertices, } 12 \text{ edges}}

🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.