Graph Algorithms - Breadth-First Search (BFS)
Introduction to graph theory and Breadth-First Search (BFS). Covers graph representations (adjacency matrix, adjacency list) and applications of BFS like shortest path finding in unweighted graphs.
Introduction to Graph Theory
What is Graph Theory?
Graph theory is a branch of mathematics that studies the relationships between objects. These objects are represented as vertices (also called nodes) and the relationships between them are represented as edges (also called links or lines). It provides a powerful and versatile way to model various real-world scenarios, from social networks and transportation systems to computer algorithms and molecular structures. Instead of focusing on physical properties or measurements, graph theory emphasizes the connections and structures formed by the relationships themselves. The primary focus is on understanding the properties of these networks and developing algorithms to solve problems related to them.
Graph theory is used in a wide variety of fields, including:
- Computer Science: Network routing, data structures, algorithm design.
- Social Sciences: Analyzing social networks, understanding influence and connections.
- Biology: Modeling protein interactions, studying disease spread.
- Operations Research: Optimizing transportation routes, scheduling tasks.
- Chemistry: Representing molecular structures and chemical reactions
Overview of Graph Theory Concepts
Vertices and Edges
The fundamental components of a graph are vertices and edges.
- Vertices (Nodes): These represent the objects or entities in the system. They are often depicted as points or circles. Examples include people in a social network, cities in a map, or websites on the internet.
- Edges (Links/Lines): These represent the relationships or connections between vertices. They are typically depicted as lines connecting two vertices. Examples include friendships in a social network, roads connecting cities, or hyperlinks between websites.
Types of Graphs
Graphs can be classified into different types based on the properties of their edges:
- Undirected Graph: In an undirected graph, edges have no direction. The relationship between two vertices is mutual. If there is an edge between vertex A and vertex B, it means that A is related to B, and B is related to A. Think of friendships on Facebook: if A is friends with B, then B is friends with A.
- Directed Graph (Digraph): In a directed graph, edges have a direction. The relationship between two vertices is not necessarily mutual. If there is an edge from vertex A to vertex B, it means that A is related to B, but it doesn't necessarily mean that B is related to A. Think of following someone on Twitter: A can follow B, but B might not follow A.
A graph can also be a combination of the above, or have properties like being weighted where edges have costs or distances associated with them.
Common Graph Terminology
Here's a glossary of essential graph theory terms:
- Adjacent: Two vertices are adjacent if they are connected by an edge.
- Degree (of a vertex): The number of edges incident to a vertex. In directed graphs, we distinguish between the in-degree (number of edges coming into the vertex) and the out-degree (number of edges going out of the vertex).
- Path: A sequence of vertices connected by edges.
- Cycle: A path that starts and ends at the same vertex.
- Connected Graph: A graph where there is a path between every pair of vertices.
- Complete Graph: A graph where every vertex is connected to every other vertex.
- Weighted Graph: A graph where each edge has a weight or cost associated with it.
- Subgraph: A graph whose vertices and edges are subsets of another graph.
- Tree: A connected graph with no cycles.
- Forest: A collection of trees.