Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

CS 512 : Data Structures and Algorithms

Dr. Sushanta Karmakar


Department of Computer Science and Engineering Indian Institute of Technology, Guwahati

July 31, 2012

Graphs

G is a way of encoding pairwaise relationship

Graphs

G is a way of encoding pairwaise relationship G = (V , E ),

Graphs

G is a way of encoding pairwaise relationship G = (V , E ), Directed / undirected graph

Graphs

G is a way of encoding pairwaise relationship G = (V , E ), Directed / undirected graph For directed edge (u, v ), it means the edge leaves u and

enters v

Examples

Transportation Network

Examples

Transportation Network Communication Network

Examples

Transportation Network Communication Network Information Network (there is an edge from u to v if u has a

hyperlink to v )

Examples

Transportation Network Communication Network Information Network (there is an edge from u to v if u has a

hyperlink to v )
Social Network

Examples

Transportation Network Communication Network Information Network (there is an edge from u to v if u has a

hyperlink to v )
Social Network Dependency Network (course prerequisites)

Paths and Connectivity

Path: a sequence of nodes v1 , v2 , v3 , . . . , vk1 , vk such that

vi , vi+1 is joined by an edge

Paths and Connectivity

Path: a sequence of nodes v1 , v2 , v3 , . . . , vk1 , vk such that

vi , vi+1 is joined by an edge


A path is simple if all the nodes are distinct

Paths and Connectivity

Path: a sequence of nodes v1 , v2 , v3 , . . . , vk1 , vk such that

vi , vi+1 is joined by an edge


A path is simple if all the nodes are distinct A cycle is a path v1 , v2 , v3 , . . . , vk1 , vk (k > 2) such that

rst k 1 nodes are distinct and v1 = vk

Connectivity

An undirected graph is called connected if for every pair

(u, v ), there is a path from u to v

Connectivity

An undirected graph is called connected if for every pair

(u, v ), there is a path from u to v


notion of strongly connectedness

Connectivity

An undirected graph is called connected if for every pair

(u, v ), there is a path from u to v


notion of strongly connectedness Distance :

distance between u and v is the minimum no of edges in a u v path

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle
delete some edge, tree becomes a disconnected graph

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor Leaf: node with no descendant

Tree

An undirected graph is a tree if it is connected and does not

contain a cycle
delete some edge, tree becomes a disconnected graph notion of parent and child descendant / ancestor Leaf: node with no descendant Every n node tree has exactly (n 1) edges

Graph Traversal

Given a graph G = (V , E ) and two nodes s and t, is there a

path from s to t

Graph Traversal

Given a graph G = (V , E ) and two nodes s and t, is there a

path from s to t
s t connectivity problem

Graph Traversal

Given a graph G = (V , E ) and two nodes s and t, is there a

path from s to t
s t connectivity problem Two natural algorithms: BFS (breadth-rst search) / DFS

(depth-rst search)

Breadth-First Search

start from node s

Breadth-First Search

start from node s search by layers L1 , L2 , L3 so on

Breadth-First Search

start from node s search by layers L1 , L2 , L3 so on until no further node can be added

Breadth-First Search

start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors

Breadth-First Search

start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors distance from s to any node in Lj is j

Breadth-First Search

start from node s search by layers L1 , L2 , L3 so on until no further node can be added Li is the i th hop neighbors distance from s to any node in Lj is j There is a path from s to t if t appears in some layer

Breadth-First Search (contd.)

Creates a tree T rooted at s on the set of nodes reachable

from s

Breadth-First Search (contd.)

Creates a tree T rooted at s on the set of nodes reachable

from s
We call T as a breadth-rst search tree (simply BFS tree)

Breadth-First Search (contd.)

Creates a tree T rooted at s on the set of nodes reachable

from s
We call T as a breadth-rst search tree (simply BFS tree) Try an example

A Property

nontree edges: either connected nodes in the same layer, or

connected nodes in adjacent layers

A Property

nontree edges: either connected nodes in the same layer, or

connected nodes in adjacent layers


Theorem : Let T be a BFS tree, let x and y be nodes in T

belonging to layers Li and Lj respectively, let (x, y ) E such that (x, y ) is a nontree edge. Then i and j dier by at most 1.

A Property

nontree edges: either connected nodes in the same layer, or

connected nodes in adjacent layers


Theorem : Let T be a BFS tree, let x and y be nodes in T

belonging to layers Li and Lj respectively, let (x, y ) E such that (x, y ) is a nontree edge. Then i and j dier by at most 1.
proof by contradiction

Connected Component

The set of nodes discovered by BFS are the nodes reachable

from s. Let this set be denoted as R

Connected Component

The set of nodes discovered by BFS are the nodes reachable

from s. Let this set be denoted as R


R is a connected component containing s

Connected Component

The set of nodes discovered by BFS are the nodes reachable

from s. Let this set be denoted as R


R is a connected component containing s Other ways to form a connected component

You might also like