Introduction to Graph Algorithms and Implementation
Explore the basics of graph algorithms by understanding graph definitions, types of graph representations, and how to implement a Graph class in C++. This lesson covers key concepts such as nodes, edges, paths, cycles, and connectivity, preparing you to handle graph problems efficiently using adjacency lists.
We'll cover the following...
Before we begin, lets briefly discuss what are graphs?
A graph is an abstract notation used to represent the connection between pairs of objects. It can be used to represent networks: systems of roads, airline flights from city to city, how the Internet is connected, or social connectivity on facebook, twitter etc. We use some standard graph algorithms to solve these otherwise difficult problems.
Representing Graphs
Graphs represent pairwise relationships between objects. Graphs are mathematical structures and therefore can be visualized by using two basic components, nodes and edges
A node, also known as a vertex, is a fundamental part of a graph. It is the entity that has a name, known as the key, and other information related to that entity. A relationship between nodes is expressed using edges. An edge between two nodes expresses a one-way or two-way relationship between the nodes.
Graphs can be represented as Adjacency Matrix and Adjacency List.
For the remainder of this course, we will be using Adjacency Lists because Algorithms can be performed more efficiently using this form of representation.
For example, the adjacency list representation allows you to easily iterate through the neighbors of a node. In the adjacency matrix representation, you will need to iterate through all the nodes to identify a node’s neighbors.
Mathematical Notation
The set of vertices of Graph is denoted by , or just if there is no ambiguity.
An edge between vertices and is written as . The set of edges of is denoted , or just if there is no ambiguity.
The graph in this picture has the vertex set = {1, 2, 3, 4, 5, 6}.
The edge set = {{1, 2}, {1, 5}, {2, 3}, {2, 5}, {3, 4}, {4, 5}, {4, 6}}.
A path in a graph is a sequence of vertices , with the property that there are edges between and . We say that the path goes from to . The sequence is a path from node to node in the graph above. A path is simple if its vertices are all different.
A cycle is a path for which
- ,
- the first vertices are all different, and,
- .
The sequence is a cycle in the graph above.
A graph is connected if for every pair of vertices and , there is a path from to .
The Graph Class
Graph class consists of two data members:
- The total number of vertices in the graph
- A list of linked lists to store adjacent vertices
So let’s get down to the implementation!
We’ve laid down the foundation of our Graph class. The variable vertices contains an integer specifying the total number of vertices.
The second component is a pointer adjacencyList, which will be assigned memory when graph constructor is called according to the number of nodes we want to create. We simply have to run a loop and create a list for each vertex.