0% found this document useful (0 votes)
13 views

Algorithms Section 9

The document provides an overview of graph theory, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including undirected, directed, weighted, cyclic, and acyclic graphs. It explains the conditions for Eulerian paths and circuits in both undirected and directed graphs, along with methods for checking their existence. Additionally, it discusses data structure methods for representing graphs, specifically adjacency matrices and adjacency lists.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Algorithms Section 9

The document provides an overview of graph theory, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including undirected, directed, weighted, cyclic, and acyclic graphs. It explains the conditions for Eulerian paths and circuits in both undirected and directed graphs, along with methods for checking their existence. Additionally, it discusses data structure methods for representing graphs, specifically adjacency matrices and adjacency lists.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS Algorithm Graph

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.

Weighted: In a weighted graph, each edge is assigned a weight or cost. Consider a


graph of 4 nodes as in the diagram below. As you can see each edge has a
weight/cost assigned to it. If you want to go from vertex 1 to vertex 3, you can
take one of the following 3 paths:

➢ 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.

How to find whether a given graph is Eulerian or not?


The problem is same as following question. “Is it possible to draw a given graph
without lifting pencil from the paper and without tracing any of the edges more
than once”.
A graph is called Eulerian if it has an Eulerian Cycle and called Semi-Eulerian if it
has an Eulerian Path. Fortunately, we can find whether a given graph has a
Eulerian Path or not in polynomial time. In fact, we can find it in O(V+E) time.
Following are some interesting properties of undirected graphs with an Eulerian
path and cycle. We can use these properties to find whether a graph is Eulerian or
not.
Eulerian Cycle
An undirected graph has Eulerian cycle if following two conditions are true.
a) All vertices with non-zero degree are connected. We don’t care about vertices
with zero degree because they don’t belong to Eulerian Cycle or Path (we only
consider all edges).
b) All vertices have even degree.
Eulerian Path
An undirected graph has Eulerian Path if following two conditions are true.
a) Same as condition (a) for Eulerian Cycle
b) If even number vertices have odd degree and all other vertices have even
degree. Note that only one vertex with odd degree is not possible in an
undirected graph (sum of all degrees is always even in an undirected graph)
Note that a graph with no edges is considered Eulerian because there are no
edges to traverse.
How does this work?
In Eulerian path, each time we visit a vertex v, we walk through two unvisited
edges with one end point as v. Therefore, all middle vertices in Eulerian Path must
have even degree. For Eulerian Cycle, any vertex can be middle vertex, therefore
all vertices must have even degrees.

Eulerian path and circuit for directed graph


For example, the following graph has an eulerian cycle as {1, 0, 3, 4, 0,

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} } )

2- draw the following Directed graph:


G=( {1,2,3,4,5}, {
(1,2),(1,4),(1,5),(2,3),(2,4),(2,5),(3,2),(3,4),(3,5),(4,1),(4,2),(4,5),(5,2),(5,3),(5,4) } )
3- describe the following graph (find G):

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} } )

4- describe the following directed graph (find G):

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) } )

Data Structure Methods for Graphs:


We can store the graph or digraph information in an adjacency matrix and adjacency list

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

You might also like