Algorithms Section 9
Algorithms Section 9
Graph
Graphs (G) are mathematical structures that represent pairwise relationships
between objects. A graph is a flow structure that represents the relationship
between various objects. It can be visualized by using the following two basic
components: vertices and edges. Each edge has two endpoints, which belong to
the vertex set. We say that the edge connects (or joins) these two vertices.
The vertex set of G is denoted V(G), or just V if there is no ambiguity.
An edge between vertices u and v is written as {u, v}. The edge set of G is denoted
E(G), or just E if there is no ambiguity.
The graph in this picture has the vertex set V = {1, 2, 3, 4, 5, 6}. The edge set E =
{{1, 2}, {1, 5}, {2, 3}, {2, 5}, {3, 4}, {4, 5}, {4, 6}}.
A self-loop is an edge whose endpoints is a single vertex. Multiple edges are two
or more edges that join the same two vertices.
A graph is called simple if it has no self-loops and no multiple edges, and a
multigraph if it does have multiple edges.
The degree of a vertex v is the number of edges that connect to v.
A path in a graph G = (V, E) is a sequence of vertices v1, v2, …, vk, with the property
that there are edges between vi and vi+1. We say that the path goes from v1 to vk.
The sequence 6, 4, 5, 1, 2 is a path from 6 to 2 in the graph above. A path is simple
if its vertices are all different.
A cycle is a path v1, v2, …, vk for which k > 2, the first k - 1 vertices are all different,
and v1 = vk. The sequence 4, 5, 2, 3, 4 is a cycle in the graph above.
A graph is connected if for every pair of vertices u and v, there is a path from u to
v.
If there is a path connecting u and v, the distance between these vertices is defined as
the minimal number of edges on a path from u to v.
A connected component is a subgraph of maximum size, in which every pair of
vertices are connected by a path. Here is a graph with three connected
components.
Types of graphs
Undirected: An undirected graph is a graph in which all the edges are bi-
directional i.e. the edges do not point in any specific direction.
Directed: A directed graph is a graph in which all the edges are uni-directional i.e.
the edges point in a single direction.
➢ 1 -> 2 -> 3
➢ 1 -> 3
➢ 1 -> 4 -> 3
Therefore the total cost of each path will be as follows: - The total cost of 1 -> 2 ->
3 will be (1 + 2) i.e. 3 units - The total cost of 1 -> 3 will be 1 unit - The total cost of
1 -> 4 -> 3 will be (3 + 2) i.e. 5 units
Cyclic: A graph is cyclic if the graph comprises a path that starts from a vertex and
ends at the same vertex. That path is called a cycle. An acyclic graph is a graph
that has no cycle.
A tree is an undirected graph in which any two vertices are connected by only one
path. A tree is an acyclic graph and has N - 1 edges where N is the number of
vertices. Each node in a graph may have one or multiple parent nodes. However,
in a tree, each node (except the root node) comprises exactly one parent node.
Note: A root node has no parent. A tree cannot contain any cycles or self-loops,
however, the same does not apply to graphs.
Eulerian path and circuit for undirected graph
Eulerian path is a path in graph that visits every edge exactly once. Eulerian
Circuit is an Eulerian Path which starts and ends on the same vertex.
2, 1}
How to check if a directed graph is eulerian?
A directed graph has an eulerian cycle if following conditions are true
1) All vertices with nonzero degree belong to a single strongly connected
component.
2) In degree is equal to the out degree for every vertex.
We can detect singly connected component using Kosaraju’s DFS based simple
algorithm.
To compare in degree and out-degree, we need to store in degree and out-degree
of every vertex. Out degree can be obtained by the size of an adjacency list. In
degree can be stored by creating an array of size equal to the number of vertices.
‘2-0 0-1 1-2 2-3’.
Exercises:
1- draw the following graph:
G=( {1,2,3,4,5,6}, {{1,2},{1,4},{2,5},{2,6},{3,4},{3,5},{3,6},{4,5},{4,6},{5,6} } )
G= ( {1,2,3,4,5,6} , { {1,2},{1,4},{1,6},{2,3},{2,4},{2,6},{3,4},{4,5},{4,6},{5,6} } )
G=( {1,2,3,4,5,6,7} ,{
(1,2),(1,3),(1,4),(2,1),(2,4),(2,7),(3,4),(4,6),(5,2),(5,7),(6,3),(6,7),(7,4),(7,5) } )
An Adjacency Matrix: gives you the ability to quickly access edge information, but if the
graph is far from being a complete graph there will be many empty elements in the
array.
An Adjacency List: use space that is proportional to the number of edges in the graph but
the time to access edge info will be greater.
20
2
9
24
10
1 3
30 15
4 5
G= ( {1,2,3,4,5},{ {1,2},{1,3},{2,3},{2,4},{3,5},{4,5} } )
1 2 3 4 5
1 0 1 1 0 0
2 1 0 1 1 0
Adjacency matrix = 3 1 1 0 0 1
4 0 1 0 0 1
5 0 0 1 1 0
1 2 3
2 1 3 4
Adjacency List =
3 1 2 5
4 2 5
5 3 4