Neural Sync Active
Graph Fundamentals
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:
- Define a graph and its components (vertices, edges)
- Distinguish between directed and undirected graphs
- Represent graphs using adjacency matrices and adjacency lists
- Compute the degree (in-degree/out-degree) of vertices
- 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) consists of:
- V: a set of vertices (nodes)
- E: a set of edges (pairs of vertices)
18.2.1 Types of Graphs
(Diagram)
| Type | Description | Edge Notation |
|---|---|---|
| Undirected | Edges have no direction | (u,v)=(v,u) |
| Directed | Edges have a direction | uโv |
| Simple | No loops, no multiple edges | Standard |
| Complete Knโ | Every pair of vertices connected | n(nโ1)/2 edges |
| Bipartite | Vertices split into 2 sets, edges only between sets |
18.3 Graph Representations
18.3.1 Adjacency Matrix
A โฃVโฃรโฃVโฃ matrix A where:
For undirected graphs: A is symmetric.
For weighted graphs: A[i][j]= weight (not just 0/1).
Example: Graph with vertices {1,2,3} and edges {(1,2),(2,3),(3,1)}
18.3.2 Adjacency List
For each vertex, store a list of its neighbors.
Example (same graph):
pseudo1 โ [2, 3] 2 โ [1, 3] 3 โ [1, 2]
| Representation | Memory | Edge Lookup | Neighbor Iteration |
|---|---|---|---|
| Matrix | O(V2) | O(1) | O(V) |
| List | O(V+E) | O(degree) | O(degree) |
18.4 Degree of Vertices
| Definition | Notation | |
|---|---|---|
| Degree (undirected) | Number of edges incident to vertex | deg(v) |
| In-degree (directed) | Number of edges entering v | degโ(v) or in(v) |
| Out-degree (directed) | Number of edges leaving v | deg+(v) or out(v) |
Handshaking Lemma: In any undirected graph, โvโVโdeg(v)=2โฃEโฃ. In a directed graph, โdegโ(v)=โdeg+(v)=โฃEโฃ.
18.5 Special Graphs
| Graph | Definition | #Edges | Example |
|---|---|---|---|
| Complete Knโ | All n(nโ1)/2 possible edges | n(nโ1)/2 | K5โ has 10 edges |
| Cycle Cnโ | n vertices in a cycle | n | C4โ is a square |
| Path Pnโ | n vertices in a line | nโ1 | |
| Bipartite Km,nโ | Complete bipartite | mโ n | K3,3โ |
18.6 Worked Examples
Example 1: Draw the graph G=(V,E) with V={1,2,3,4} and E={(1,2),(2,3),(3,4),(4,1),(1,3)}. Write its adjacency matrix.
Example 2: For a directed graph with edges 1โ2, 2โ3, 3โ1, 1โ3, find in-degrees and out-degrees.
| Vertex | In-degree | Out-degree |
|---|---|---|
| 1 | 1 (from 3) | 2 (to 2, 3) |
| 2 | 1 (from 1) | 1 (to 3) |
| 3 | 2 (from 1, 2) | 1 (to 1) |
Verify: โin=โout=4=โฃEโฃ โ
๐ Key Formulas โ Summary Table
| Concept | Formula | Notes |
|---|---|---|
| Complete graph edges | n(nโ1)/2 | Knโ |
| Bipartite edges | mโ n | Km,nโ |
| Handshaking (undirected) | $\sum \deg(v) = 2 | E |
| Handshaking (directed) | $\sum \text{in}(v) = \sum \text{out}(v) = | E |
| Adjacency matrix size | VรV | |
| Adjacency list memory | O(V+E) |
โ ๏ธ Common Pitfalls
Pitfall 1: Confusing Directed and Undirected Edge Notation
In directed: (u,v) means uโv, not vโu. In undirected: (u,v)=(v,u).
Pitfall 2: Forgetting Symmetry in Undirected Adjacency Matrix
Undirected graphs have symmetric adjacency matrices: 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
>A=โ001โ100โ010โโ>Q1: How many edges does K6โ have?6(5)/2=1515โ Q2: Draw the adjacency matrix for a directed graph with 1โ2, 2โ3, 3โ1.
>โ001โ100โ010โโโ>
>A=โ0101โ1010โ0101โ1010โโ>Q3: In an undirected graph with sum of degrees = 24, how many edges?2โฃEโฃ=24โนโฃEโฃ=1212โ Q4: List the adjacency list for K3โ.1โ[2,3], 2โ[1,3], 3โ[1,2]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โ Q6: What is the sum of in-degrees in any directed graph with 10 edges?1010โ Q7: Draw C4โ and give its adjacency matrix.
Cycleย graphย withย 4ย verticesโ Q8: How many vertices does K3,4โ have? How many edges?Vertices: 3+4=7, Edges: 3โ 4=127ย vertices,ย 12ย edgesโ
๐ Cross-References
- Next topic: Graph Traversal โ BFS, DFS
- Related: DAGs & Topological Sort
- Across courses: BSCS1001 Computational Thinking (graph algorithms); BSMA1003 Maths 2 (graph theory in linear algebra) Join Discord Previous9.3 Integration TechniquesNext10.2 Graph Traversal (BFS/DFS)