0% found this document useful (0 votes)
70 views21 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses graphs and graph algorithms. It begins with definitions of graphs, including vertices, edges, and different graph types like directed and undirected graphs. It then discusses common graph terminology like walks, paths, and cycles. Next, it covers different data structures for representing graphs, including adjacency lists and matrices. Finally, it provides examples of common graph algorithmic problems and highlights finding a sink vertex as an interesting problem that can be solved in O(n) time using an adjacency matrix representation.

Uploaded by

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

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses graphs and graph algorithms. It begins with definitions of graphs, including vertices, edges, and different graph types like directed and undirected graphs. It then discusses common graph terminology like walks, paths, and cycles. Next, it covers different data structures for representing graphs, including adjacency lists and matrices. Finally, it provides examples of common graph algorithmic problems and highlights finding a sink vertex as an interesting problem that can be solved in O(n) time using an adjacency matrix representation.

Uploaded by

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

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 19
Graphs
Definitions, notations, and terminologies
Data structures for graphs
A few algorithmic problems in graphs
1
Why Graphs ??
2
Finding shortest route between cities
3









Given a network of roads connecting various cities, design an algorithm to
compute the shortest route between any two cities.
Just imagine how you would solve/approach this problem.
Embedding an integrated circuit on
mother board
4









How would you embed ports of various ICs on a plane and make connections among
them so that
No two connections intersect each other
The total length of all the connections is minimal
A social network or world wide web (WWW)
5







Can we make some useful observations about the structure (diameter, degree
distribution, etc.) of such networks ?



The three applications can be modeled as Graph
Vertices
Edges
Graph
7
Definitions, notations, and terminologies
Graph


A graph G is defined by two sets
V : set of vertices
E : set of edges
Note that E (VV)


Notation:
A graph consisting of vertices V and edges E is denoted by G = (V,E)

8
Types of graphs
Undirected Graph Directed Graph
9





V= {1,2,3,4,5,6}
E= {(1,2), (1,5),
(2,5), (2,3),
(3,4),
(4,5), (4,6)}





V= {0,1,2,3,4,5}
E= { (0,1), (0,4),
(1,2),
(2,0), (2,1),
(3,2),
(4,5),
(5,4) }




Notations


Notations:
= |V|
m = |E|

Note: For directed graphs, m ??.

For undirected graphs, m ??

10
(-1)
(-1)/2
Terminologies

Walk: A sequence <

, ,

> of vertices in a graph is said to be a walk from


to if
=


For each <, (

,
+
) E

Path: A walk <

, ,

> on which no vertex appears twice.


(two vertices are said to be connected if there is a path between them)

Cycle: A walk <

, ,

> where no intermediate vertex gets repeated and




11
Terminologies






<1,5,4> is a walk from 1 to 4.
<1,3,2,5> is not a walk.
<1,2,5,2,3,4,5,4,6> is a walk from 1 to 6.
<1,2,5,4,6> is a path from 1 to 6.
<2,3,4,5,2> is a cycle.
12
Terminologies







Connected component:
A maximal subset of connected vertices
You can not add any more vertex to the subset
and still keeping it connected.
Data Structures for Graphs
14
Link based data structure for graph
Undirected Graph Adjacency Lists
15





V= {1,2,3,4,5,6}
E= {(1,2), (1,5),
(2,5), (2,3),
(3,4),
(4,5), (4,6)}









1 3 5
2 5
2 4
4
3 5 6
1 2 4
1
2
3
4
5
6
Size = O(n+m)
Link based data structure for graph


Advantage of Adjacency Lists :
Space efficient
All the neighbors of a vertex can be computed in optimal time.

Disadvantage of Adjacency Lists :
To determine if there is an edge from x to y, it may require scanning the
entire adjacency list of vertex x (Hence O(n) time in the worst case).
16
Array based data structure for graph
Undirected Graph Adjacency Matrix
17





V= {1,2,3,4,5,6}
E= {(1,2), (1,5),
(2,5), (2,3),
(3,4),
(4,5), (4,6)}









1
2
3
4
5
6
0 1 0 0 1 0
1 2 3 4 5 6
1 0 1 0 1 0
0 1 0 1 0 0
0 0 1 0 1 1
1 1 0 1 0 0
0 0 0 1 0 0
Size = O(
2
)
Array based data structure for graph


Advantage of Adjacency Matrix :
It takes O(1) time to determine whether there is an edge from x to y for
any two vertices x and y.


Disadvantage of Adjacency Matrix :
It takes O() time to compute/enumerate all neighbors of a given vertex x.
It takes O(

) space. So we can not store Adjacency Matrix in these days


RAM even for graphs having 1 million vertices.

18
Which data structure is commonly used for
storing graphs ?
Majority of applications of graphs store them in adjacency lists mainly due to
the following compelling reasons.

Graphs in real life are sparse (

). So Adjacency lists representation


offers a much more compact storage compared to Adjacency matrix
representation.

Most of the algorithms on graphs require enumerating and processing all
neighbors of each vertex. As a result adjacency matrix will enforce O(

)
bound on the time complexity for the algorithm irrespective of the other
computation.

**There are some exceptions where the algorithm does not require enumeration of all
neighbors of each vertex. One such exception is discussed at the end of this lecture.


19
A sample of Graph algorithmic Problems
Almost every second algorithm is a graph algorithms due to wide applications
of graphs for modeling various computational problems. The student is
encouraged to ponder over the following problems. All of them and many
more will be discussed during this course and the course CS345.

Determine if there is a path between any two given vertices in a graph.
Compute all connected components in a graph.
Determine if there is a cycle in a graph.
Compute the largest possible set of edges from the graph whose removal
does not destroy the connectivity of the graph
Compute a path of shortest length between two vertices.
Determine if there is a cycle passing through all vertices.
Compute the cycle of shortest length in a graph.
20
An interesting problem
(Finding a sink)

A vertex x in a given directed graph is said to be a sink if
There is no edge emanating from (or leaving) x
Every other vertex has an edge into x.

Given a directed graph G=(V,E) given in an adjacency matrix representation,
design an O(n) time algorithm to determine if there is any sink in G.

You are encouraged to solve the problem mentioned above. It does not require
anything more than what has been discussed in this lecture. We shall discuss its
solution in the next class.

21

You might also like