Module 9 - Graphs
Module 9 - Graphs
1
Module 9: Graphs
Introduction to Graphs
A graph is a non-linear kind of data structure made up of nodes or vertices and edges. The
edges connect any two nodes in the graph, and the nodes are also known as vertices.
Königsberg bridge problem, a recreational mathematical puzzle, set in the old Prussian city
of Königsberg (now Kaliningrad, Russia), that led to the development of the branches of
mathematics known as topology and graph theory. In the early 18th century, the citizens
of Königsberg spent their days walking on the intricate arrangement of bridges across the
waters of the Pregel (Pregolya) River, which surrounded two central landmasses connected
by 7 bridges. According to folklore, the question arose of whether a citizen could take a
walk through the town in such a way that each bridge would be crossed exactly once.
In 1735 the Swiss mathematician Leonhard Euler used graph theory to solve Seven
Bridges of Königsberg problem. “Is there a possible way to traverse every bridge exactly
once?” – Euler
A B
D
Leonhard Euler presented a solution to this problem, concluding that such a walk was
impossible.
Course Module
It would be nearly 150 years before mathematicians would picture the Königsberg bridge
problem as a graph consisting of nodes (vertices) representing the landmasses and arcs
(edges) representing the bridges. The degree of a vertex of a graph specifies the number of
edges incident to it.
Defining the degree of a vertex to be the number of edges incident to it, Euler showed that
there is a walk starting at any vertex, going through each edge exactly once and terminating
at the start vertex if the degree of each, vertex is even. A walk which does this is called
Eulerian Walk. (Conditions: The Degree is Even; Zero Odd Vertex or 2 Odd Vertex). There is
no Eulerian walk for the Koenigsberg bridge problem as all four vertices are of odd degree.
A graph contains a set of points known as nodes (or vertices) and set of links known as
edges (or Arcs) which connects the vertices. A graph G is represented as G = ( V , E ), where
V is set of vertices and E is set of edges.
Example:
Graph G can be defined as G = ( V , E ). Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(B,E),(C,D),(E,D)}. This is a graph with 5 vertices and 7
edges.
Graph Terminology
• Vertex: An individual data element of a graph is called as Vertex. Vertex is also known as
node. In above example graph, A, B, C, D & E are known as vertices.
• Edge: An edge is a connecting link between two vertices. Edge is also known as Arc. An edge
is represented as (starting Vertex, ending Vertex). In above graph, the link between vertices
A and B is represented as (A,B).
Types of Graphs
• Undirected Graph: An undirected graph comprises a set of nodes and links connecting them.
The order of the two connected vertices is irrelevant and has no direction. You can form an
undirected graph with a finite number of vertices and edges.
• Directed Graph: A directed graph also referred to as a digraph, is a set of nodes connected by
edges, each with a direction.
• Complete Graph: If a graph G= (V, E) is also a simple graph, it is complete. Using the edges,
with n number of vertices must be connected. It's also known as a full graph because each
vertex's degree must be n-1.
Course Module
• Cyclic or Cycle Graph: A graph having cycle is called cycle graph. In this case the first and last
nodes are the same. A closed simple path is a cycle.
• Acyclic Graph: When there are no cycles in a graph, it is called an acyclic graph.
• Weighted Graph: A graph G= (V, E) is called a labeled or weighted graph because each edge
has a value or weight representing the cost of traversing that edge.
• Simple Graph: If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a
simple graph. As a result, there is just one edge linking two vertices, depicting one-to-one
interactions between two elements.
• Length of a path: The number of edges in a path is called the length of that path. In the above
diagram, the length of the path is 5.
The most frequent graph representations are the two that follow:
• Adjacency matrix
• Adjacency list
Adjacency Matrix
• A sequential representation is an adjacency matrix.
• It's used to show which nodes are next to one another. I.e., is there any connection between
nodes in a graph?
• You create an M X M matrix G for this representation. If an edge exists between vertex a and
vertex b, the corresponding element of G, gi,j = 1, otherwise gi,j = 0.
• If there is a weighted graph, you can record the edge's weight instead of 1s and 0s.
IT07-IT07L Data Structure and Algorithm
7
Module 9: Graphs
Course Module
Adjacency List
• A linked representation is an adjacency list.
• You keep a list of neighbors for each vertex in the graph in this representation. It means that
each vertex in the graph has a list of its neighboring vertices.
• You have an array of vertices indexed by the vertex number, and the corresponding array
member for each vertex x points to a singly linked list of x's neighbors.