3. Algorithms

Graph Fundamentals

Cover graph representations, traversal algorithms (BFS, DFS), and basic pathfinding concepts for network-style problems.

Graph Fundamentals

Hi students! 👋 Welcome to one of the most exciting topics in computer science - graphs! In this lesson, you'll discover how graphs help us solve real-world problems like finding the shortest route on Google Maps 🗺️, suggesting friends on social media 👥, or even analyzing computer networks. By the end of this lesson, you'll understand different ways to represent graphs, master two essential traversal algorithms (BFS and DFS), and explore basic pathfinding concepts that power many modern applications.

Understanding Graphs: More Than Just Pictures

A graph in computer science isn't the bar charts or line graphs you might know from math class. Instead, it's a powerful data structure that represents relationships between different entities. Think of it as a network of connections! 🌐

A graph consists of two main components:

  • Vertices (or Nodes): These are the individual points or entities in your graph
  • Edges: These are the connections or relationships between vertices

Imagine Facebook's friend network - each person is a vertex, and each friendship is an edge connecting two people. Or consider a city's road system where intersections are vertices and roads are edges connecting them.

Graphs can be directed (like Twitter follows - you can follow someone who doesn't follow you back) or undirected (like Facebook friendships - it goes both ways). They can also be weighted (where edges have values, like distances between cities) or unweighted (where all connections are equal).

Real-world applications are everywhere! Google uses graphs to rank web pages, Netflix uses them for recommendations, and GPS systems use them to find optimal routes. According to recent studies, graph algorithms process over 2.5 quintillion bytes of data daily across major tech companies! 📊

Graph Representations: Storing Connections Efficiently

Now that you understand what graphs are, let's explore how computers actually store them. There are two primary methods, each with unique advantages.

Adjacency Matrix Representation

An adjacency matrix is like a giant table where both rows and columns represent vertices. If there's an edge between vertex i and vertex j, we put a 1 (or the edge weight) at position [i][j]. If there's no connection, we put a 0.

For example, if we have 4 vertices (A, B, C, D) and edges A-B, B-C, and C-D, our matrix would look like:

    A  B  C  D
A [ 0  1  0  0 ]
B [ 1  0  1  0 ]
C [ 0  1  0  1 ]
D [ 0  0  1  0 ]

Advantages: Super fast to check if two vertices are connected - just look at matrix[i][j]! It takes constant time O(1).

Disadvantages: Uses lots of memory - specifically O(V²) space where V is the number of vertices. For sparse graphs (few edges), you're wasting space storing mostly zeros.

Adjacency List Representation

An adjacency list stores each vertex along with a list of its neighbors. It's like having a contact list where each person has their own list of friends.

Using the same example:

  • A: [B]
  • B: [A, C]
  • C: [B, D]
  • D: [C]

Advantages: Memory efficient for sparse graphs - only uses O(V + E) space where E is the number of edges. Most real-world graphs are sparse!

Disadvantages: Checking if two specific vertices are connected takes O(degree) time, where degree is the number of connections a vertex has.

According to research from major tech companies, adjacency lists are preferred for about 80% of graph applications because most real-world networks are sparse - think about it, you're not friends with everyone on Facebook! 📱

Depth-First Search (DFS): Going Deep Before Going Wide

Depth-First Search is like being an explorer who always chooses to go as far as possible down one path before backtracking. It's a fundamental algorithm that visits vertices by going as deep as possible before exploring other branches.

Here's how DFS works:

  1. Start at a chosen vertex and mark it as visited
  2. Explore an unvisited neighbor and recursively apply DFS
  3. If no unvisited neighbors exist, backtrack to the previous vertex
  4. Repeat until all reachable vertices are visited

The algorithm can be implemented using either recursion (which uses the call stack) or explicitly using a stack data structure. The time complexity is O(V + E) where V is vertices and E is edges.

Real-world applications of DFS include:

  • Maze solving: DFS naturally explores paths until it hits dead ends, then backtracks
  • Topological sorting: Ordering tasks with dependencies (like course prerequisites)
  • Detecting cycles: Finding loops in networks or dependency chains
  • Web crawling: Search engines use DFS-like approaches to explore website links

Fun fact: DFS was first described by French mathematician Charles Pierre Trémaux in the 19th century as a strategy for solving mazes! 🏰 Today, it processes millions of web pages daily in search engine crawlers.

Breadth-First Search (BFS): Exploring Layer by Layer

Breadth-First Search takes the opposite approach - it's like ripples spreading out in a pond. BFS explores all vertices at distance 1, then all vertices at distance 2, and so on.

Here's the BFS process:

  1. Start at a chosen vertex and add it to a queue
  2. While the queue isn't empty:
  • Remove a vertex from the front of the queue
  • Add all unvisited neighbors to the back of the queue
  • Mark them as visited

BFS uses a queue (first-in-first-out) data structure and also has O(V + E) time complexity.

Key applications of BFS include:

  • Shortest path finding: BFS guarantees the shortest path in unweighted graphs
  • Social network analysis: Finding degrees of separation (like "six degrees of Kevin Bacon")
  • Network broadcasting: Efficiently spreading information through networks
  • Level-order tree traversal: Processing tree nodes level by level

The most famous application might be in GPS navigation systems. While modern GPS uses more sophisticated algorithms, BFS principles help find routes by exploring nearby roads before distant ones. Companies like Google Maps process over 1 billion route requests daily using graph algorithms! 🚗

Basic Pathfinding: Finding the Best Route

Pathfinding is about discovering the best route between two points in a graph. While BFS finds the shortest path in unweighted graphs, real-world scenarios often involve weighted edges (like distances, costs, or time).

Dijkstra's Algorithm is the gold standard for shortest path problems with non-negative weights. It works by:

  1. Starting at the source vertex with distance 0
  2. Maintaining a priority queue of vertices ordered by their current shortest distance
  3. For each vertex, updating the distances to its neighbors if a shorter path is found
  4. Continuing until all vertices are processed

The algorithm guarantees the optimal solution and runs in O((V + E) log V) time with proper data structures.

A* Algorithm enhances Dijkstra's by adding a heuristic - an educated guess about the remaining distance to the destination. This makes it incredibly efficient for applications like:

  • Video game AI pathfinding (characters navigating game worlds)
  • Robot navigation (autonomous vehicles and drones)
  • Network routing protocols (internet data packet routing)

Modern applications are mind-blowing! Uber processes over 15 million trips daily using sophisticated pathfinding algorithms. Video games like Grand Theft Auto use A* to make NPCs navigate realistic city environments. Even delivery drones use these concepts to plan optimal flight paths! 🚁

Conclusion

Graphs are fundamental data structures that model relationships and connections in our digital world. You've learned how adjacency matrices offer fast connection checking while adjacency lists provide memory efficiency for sparse graphs. DFS explores deeply before backtracking, making it perfect for maze-solving and cycle detection, while BFS explores layer by layer, guaranteeing shortest paths in unweighted graphs. These traversal algorithms form the foundation for more advanced pathfinding techniques like Dijkstra's and A* algorithms, which power everything from GPS navigation to social media friend suggestions. Understanding these concepts gives you the tools to solve complex network problems and appreciate the algorithms working behind your favorite applications!

Study Notes

• Graph: Data structure with vertices (nodes) connected by edges representing relationships

• Directed vs Undirected: Directed edges have direction (like Twitter follows), undirected go both ways (like Facebook friends)

• Weighted vs Unweighted: Weighted edges have values (distances, costs), unweighted are all equal

• Adjacency Matrix: 2D array representation, O(1) connection checking, O(V²) space complexity

• Adjacency List: Each vertex stores list of neighbors, O(V + E) space, efficient for sparse graphs

• DFS (Depth-First Search): Explores as deep as possible before backtracking, uses stack/recursion

• BFS (Breadth-First Search): Explores layer by layer, uses queue, finds shortest path in unweighted graphs

• Time Complexity: Both DFS and BFS are O(V + E) where V = vertices, E = edges

• Dijkstra's Algorithm: Finds shortest path with non-negative weights, O((V + E) log V) complexity

A* Algorithm: Enhanced pathfinding using heuristics for faster route finding

• Applications: GPS navigation, social networks, web crawling, game AI, network routing

Practice Quiz

5 questions to test your understanding

Graph Fundamentals — AS-Level Computer Science | A-Warded