Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Graph Theory Basics

Introduction

Graph Theory is a branch of mathematics that studies graphs, which are mathematical structures used to model pairwise relations between objects. A graph in this context is made up of vertices (also called nodes or points) which are connected by edges (also called links or lines).

Basic Terminologies

Before diving into more complex concepts, it's essential to understand some basic terminologies used in graph theory:

  • Vertex (Node): A fundamental unit of which graphs are formed.
  • Edge (Link): A connection between two vertices.
  • Degree: The number of edges connected to a vertex.
  • Path: A sequence of edges that connects a sequence of vertices.
  • Cycle: A path that starts and ends at the same vertex.

Types of Graphs

Graphs can be categorized into several types based on their properties:

  • Undirected Graph: A graph where edges have no direction.
  • Directed Graph (Digraph): A graph where edges have directions.
  • Weighted Graph: A graph where edges have weights associated with them.
  • Unweighted Graph: A graph where edges do not have weights.
  • Simple Graph: A graph with no loops and no more than one edge between any two vertices.
  • Complete Graph: A graph where there is an edge between every pair of vertices.

Representation of Graphs

Graphs can be represented in various ways. The two most common representations are:

Adjacency Matrix

An adjacency matrix is a 2D array of size VxV where V is the number of vertices in the graph. If there is an edge between vertices i and j, the matrix value at [i][j] is 1, otherwise, it is 0.

For example, consider a graph with 3 vertices and edges (0-1), (1-2):

0 1 0
1 0 1
0 1 0

Adjacency List

An adjacency list is an array of lists. The size of the array is equal to the number of vertices. Each element of the array is a list of all adjacent vertices of a vertex.

For example, consider the same graph:

0: 1
1: 0, 2
2: 1

Graph Traversal

Graph traversal refers to the process of visiting all the nodes in a graph. There are two primary methods of graph traversal:

Breadth-First Search (BFS)

BFS starts at a chosen node and explores all its neighbors at the present depth level before moving on to nodes at the next depth level.

Consider the following graph:

0 - 1
| |
2 - 3

BFS starting from node 0: 0, 1, 2, 3

Depth-First Search (DFS)

DFS starts at a chosen node and explores as far as possible along each branch before backtracking.

Consider the same graph:

0 - 1
| |
2 - 3

DFS starting from node 0: 0, 1, 3, 2

Applications of Graph Theory in Machine Learning

Graph theory has several important applications in machine learning, including:

  • Social Network Analysis: Analyzing relationships and influences in social networks.
  • Recommendation Systems: Leveraging graph-based algorithms to recommend items to users.
  • Clustering: Grouping similar data points using graph-based clustering methods.
  • Graph Neural Networks (GNNs): Using neural networks to perform tasks on graph-structured data.

Conclusion

Graph theory is a powerful tool in both mathematics and computer science, with numerous applications in machine learning. Understanding the basics of graph theory can provide a solid foundation for exploring more advanced topics and techniques in the field.