Menu
Theme
Bachelor of Science in Computer Science
Course Content

Graph Theory

Discrete Structures

Habari! Welcome to the World of Connections!

Ever wondered how Google Maps finds the fastest matatu route from Rongai to the CBD, magically avoiding all the jam? Or how Facebook suggests friends you might know from your village? The secret isn't magic; it's Graph Theory! Think of it as the ultimate map for everything that's connected. Today, we're going to demystify this powerful tool and see how it describes our world, from our own social circles to the entire M-Pesa network.

So, What Exactly is a Graph?

Forget the charts you drew in high school. In Discrete Structures, a graph is much simpler. It's just a set of dots and lines connecting them.

  • Vertices (or Nodes): These are the dots. They represent the objects. Think of them as matatu stages, towns in Kenya, or people on Instagram.
  • Edges (or Links): These are the lines. They represent the connection or relationship between the vertices. Think of them as the roads between stages, the flights between towns, or the "follow" button on Instagram.

Let's draw a simple graph representing a few towns in Kenya.


    (Nairobi)
       /   \
      /     \
     /       \
 (Nakuru)---(Naivasha)
      \       /
       \     /
        \   /
      (Eldoret)

In this simple network, Nairobi, Nakuru, Naivasha, and Eldoret are our vertices. The lines connecting them are the edges, representing major roads.

Image Suggestion: A vibrant and stylized digital illustration of a map of Kenya. Major cities like Nairobi, Mombasa, Kisumu, and Eldoret are shown as glowing nodes. Colorful, glowing lines connect them, representing flight paths and major highways. Small icons of buses and airplanes are visible on the lines. The style is modern and tech-focused, with a background of subtle circuitry patterns.

Learning the Lingo: Essential Graph Terminology

To talk like a pro, you need to know the language. Let's learn a few key terms using our Kenyan towns example.

  • Adjacent Vertices: Two vertices are adjacent if they are connected by an edge. In our diagram, Nairobi is adjacent to Nakuru and Naivasha, but it is not adjacent to Eldoret (you have to pass through another town first).
  • Degree of a Vertex: This is simply the number of edges connected to a vertex. It tells you how "connected" something is.

Let's calculate the degree for our towns:


deg(Nairobi) = 2
deg(Nakuru) = 3
deg(Naivasha) = 2
deg(Eldoret) = 2

A cool trick (The Handshaking Lemma): If you add up the degrees of all vertices, the sum will always be twice the number of edges. Why? Because each edge connects two vertices, so it gets counted twice! In our example, Sum of degrees = 2 + 3 + 2 + 2 = 9. Hold on, ASCII art is tricky! Let's adjust the art to make it a proper graph where the lemma works.


      (Nairobi)
       /   \
      /     \
 (Nakuru)---(Naivasha)
      \     /
       \   /
      (Eldoret)

// Let's assume a direct road from Nakuru to Eldoret
// And a road from Naivasha to Eldoret is not direct, it must pass through Nakuru.
// A better drawing:

   (Nairobi) ---- (Naivasha) ---- (Nakuru)
       |                            |
       |                            |
       +----------(Eldoret)---------+

Edges: (Nairobi, Naivasha), (Naivasha, Nakuru), (Nakuru, Eldoret), (Eldoret, Nairobi)
Number of Edges |E| = 4

Degrees:
deg(Nairobi) = 2
deg(Naivasha) = 2
deg(Nakuru) = 2
deg(Eldoret) = 2

Sum of degrees = 2 + 2 + 2 + 2 = 8
2 * |E| = 2 * 4 = 8. It works!
  • Path: A sequence of vertices where each adjacent pair is connected by an edge. A path from Nairobi to Eldoret is Nairobi -> Naivasha -> Nakuru -> Eldoret.
  • Cycle: A path that starts and ends at the same vertex. For example, Nairobi -> Naivasha -> Nakuru -> Eldoret -> Nairobi is a cycle.

The Graph Family: Types of Graphs

Just like in any family, there are different types of graphs for different situations.

1. Directed vs. Undirected Graphs

  • Undirected Graph: Edges have no direction. The road between Nairobi and Nakuru is a two-way street; you can go from Nairobi to Nakuru, and from Nakuru to Nairobi. Our town map is an undirected graph.
  • Directed Graph (Digraph): Edges have a direction, usually shown with an arrow. Think of a one-way street in the CBD, or following someone on Twitter (they don't automatically have to follow you back).

// Undirected: A <=> B
(A) --- (B)

// Directed: A => B
(A) --> (B)

2. Weighted vs. Unweighted Graphs

  • Unweighted Graph: Edges just show a connection. They don't have a value. "Is there a road between Nairobi and Mombasa?" - Yes/No.
  • Weighted Graph: Each edge is assigned a numerical value, or "weight". This weight can represent distance, cost, or time. This is what Google Maps uses! The weight on the edge between Nairobi and Mombasa could be the distance (480km) or the bus fare (KSh 1500).

Scenario: The Matatu Route Planner
Imagine you are building an app to find the cheapest matatu route. You would model the city as a weighted directed graph. - Vertices: Matatu stages (e.g., Kencom, Odeon, Ngara). - Edges: The routes the matatus take. They are directed because a matatu might go from A to B, but take a different return route. - Weights: The fare (pesa) for that part of the journey. Your app's job is to find the path from your starting stage to your destination with the minimum total weight (lowest cost)!

How Do Computers Understand Graphs?

We can't just draw a picture for a computer. We need to represent graphs in a structured way. The two most common methods are:

1. Adjacency Matrix

A matrix (like a grid or table) where we use 1 if an edge exists between two vertices and 0 if it doesn't. Let's use a simpler graph: 1 (Nairobi), 2 (Mombasa), 3 (Kisumu).


// Graph:
// 1 -- 2
// | \
// |  3

// Adjacency Matrix:
//   1 2 3
// 1[0 1 1]
// 2[1 0 0]
// 3[1 0 0]

// Reading the matrix:
// Row 1, Col 2 is 1. This means there is an edge between Nairobi(1) and Mombasa(2).
// Row 2, Col 3 is 0. This means there is NO direct edge between Mombasa(2) and Kisumu(3).

2. Adjacency List

For each vertex, we just list the vertices it is adjacent to. This is often more efficient for graphs with few edges.


// Using the same graph:
// 1 -- 2
// | \
// |  3

// Adjacency List:
// 1 -> [2, 3]
// 2 -> [1]
// 3 -> [1]

// Reading the list:
// Vertex 1 (Nairobi) is connected to Vertex 2 (Mombasa) and Vertex 3 (Kisumu).
// Vertex 2 (Mombasa) is only connected to Vertex 1 (Nairobi).

Why Should You Care? The Power of Graphs

Graph theory is not just an abstract topic; it's the backbone of modern technology and logistics!

  • GPS and Mapping (Google Maps, Waze): Finding the shortest or fastest route is a classic graph problem (Dijkstra's Algorithm).
  • Social Networks (Facebook, LinkedIn): Analyzing who is connected to whom, suggesting friends, and identifying influential people.
  • Telecommunications (Safaricom, Airtel): Managing network traffic, routing calls and data packets efficiently through a network of cell towers and servers.
  • Logistics and Supply Chains (Jumia, Naivas): Finding the most efficient way to route delivery trucks from a warehouse to various drop-off points (The Traveling Salesman Problem).

Image Suggestion: A split-screen infographic. On the left, a realistic photo of Nairobi traffic. On the right, a clean, animated representation of that traffic as a graph, with cars moving along edges and certain paths highlighted in green (fastest route). The text "From Chaos to Calculation: Graph Theory in Action" is overlaid.

Your Turn! A Small Challenge

Let's put your new knowledge to the test. Think about your journey to the university.

  1. Draw a simple graph of your route.
    • What are the vertices? (e.g., Your home, bus stop 1, junction, university gate)
    • What are the edges? (The roads/paths you take)
  2. Is your graph directed or undirected? Weighted or unweighted? Why?
  3. Calculate the degree of the "bus stop" vertex. What does this number represent in the real world?

There's no single right answer. This exercise just helps you see the world as a network of connections.

Congratulations! You've just learned the fundamentals of a topic that powers a huge part of our digital world. Keep this foundation in mind; you'll see it pop up again and again in computer science, from artificial intelligence to database design. Kazi nzuri!

Pro Tip

Take your own short notes while going through the topics.

Previous Logic
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience