Graphs: Concepts, Representations, and Traversals, Slides of Data Structures and Algorithms

An introduction to graphs, their terminology, applications, and various representations. It covers undirected and directed graphs, their differences, and graph traversals such as depth first search (dfs) and breadth first search (bfs). The document also includes examples and pseudo-code for graph traversals.

Typology: Slides

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm & Data Structures
gy,
What is a Graph?
What is a Graph?
A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
El
E
xamp
l
e:
abV= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b ) ( d) ( )
c
(b
,e
)
,
(
c,
d)
,
(
c,e
)
,
(d,e)}
de
Applications
Applications
CS16
electronic circuits
CS16
networks (roads, flights,
communications)
karachi
LAX
Islamabad Abbott
Lahore
Maree
Lahore
Muza-
rabad
Terminology:
Adjacent and Incident
Adjacent and Incident
If (v
0
v
1
) is an edge in an undirected graph
If (v
0
,
v
1
) is an edge in an undirected graph
,
v0and v1are adjacent
The edge (v
0
v
1
) is incident on vertices v
0
and v
1
The edge (v
0
,
v
1
) is incident on vertices v
0
and v
1
If <v0, v1> is an edge in a directed graph
i
dj t t
d
i
dj t f
v
0
i
s
a
dj
acen
t t
o
v
1,
an
d
v1
i
s
a
dj
acen
t f
rom
v
0
The edge <v0, v1> is incident on v0and v1
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Graphs: Concepts, Representations, and Traversals and more Slides Data Structures and Algorithms in PDF only on Docsity!

Algorithm & Data Structures

gy,

What is a Graph?What is a Graph? A graph G = (V,E) is composed of:V: set of vertices^ E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices E lExample:^ a^

b^ V= {a,b,c,d,e}^ E= {(a,b),(a,c),(a,d),(b^

) (^ d) (^ ) c^

(b,e),(c,d),(c,e),(d,e)} d^

e

ApplicationsApplications

CS

electronic circuits

CS

networks (roads, flights,communications)

karachi Islamabad^ LAX

Abbott Lahore

Maree Lahore

Muza-rabad

Terminology:Adjacent and IncidentAdjacent and Incident If (v^0 v^1 ) is an edge in an undirected graphIf (v^0 , v^1 ) is an edge in an undirected graph,^ v^0 and v^1 are adjacent^ The edge (v

0 v^1 ) is incident on vertices v

0 and v^1

The edge (v^0

, v^1 ) is incident on vertices v

0 and v^1

If <v^0 , v^1 > is an edge in a directed graphi^ dj^

t t^ d^

i^ dj^ t f v^0 is adjacent to v

1 , and v^1 is adjacent from v

0

The edge <v^0

, v^1 > is incident on v

0 and v^1 docsity.com

Terminology:Degree of a VertexDegree of a Vertex^ The degree of a vertex is the number of edgesincident to that vertexdi^

d^ h For directed graph,^ the in-degree of a vertex

v^ is the number of edges that have^ v^ as the headthat have^ v^ as the head the out-degree of a vertex

v^ is the number of edges that have^ v^ as the tail

Examples

3

2

p

G^1

1 1 1 1 G^2

3 3

G^2

directed graph 30 in:1, out: 1 in-degreeout-degree^

in: 1, out: 2 1 in: 1, out: 0 (^2) G 3

Terminology:PathPath^ h^

f path:^ sequence ofvertices v^ ,v^1

,.. .vsuch k^ that consecutive

3

2

that consecutive vertices v^ and vi^

arei+ adjacent.

3

j

3

3 a^

a^ b

b c ed

c ed 7 d^

e^ d^

e a b e d c^

b e d c

Graphs: Examplep^

p 1 6

2 3

6 7 4 5 Legend^

8

Directional path

Bi-directional paths

docsity.com

Oriented (Directed) Graph

p

A graph where edges are directedA graph where edges are directed

Directed vs. Undirected Graph

p

An^ undirected graph

is one in which the pair An^ undirected graph

is one in which the pair of vertices in a edge is unordered, (v

0 , v^1 ) =

(v^1 v^0 )(v^1 ,v^0 ) A directed graph is one in which each edge isa directed pair of vertices <v

v > != a directed pair of vertices, <v

0 , v^1 > != <v

1 ,v^0 > t il^

h^ d tail^

head

Graph RepresentationsGraph Representations^ Adjacency Matrix^ Adjacency Lists

Adjacency MatrixAdjacency Matrix^ Let G=(V,E) be a graph with n vertices.h^ dj^

i^ f^ i^

di^ i^

l

The adjacency matrix of G is a two-dimensionaln by n array, say adj_mat If the edge (v

i^ , vj^ ) is in E(G), adj_mat[i][j]= If there is no such edge in E(G), adj

mat[i][j]=

t e e s^ o suc

edge^

(G), adj_^

at[ ][j]^0

The adjacency matrix for an undirected graph issymmetric; the adjacency matrix for a digraphsymmetric; the adjacency matrix for a digraphneed not be symmetric

docsity.com

Examples for Adjacency Matrixp^

f^ j^

y (^0 01 23 10111) ⎡⎤ ⎢⎥ (^1011) ⎢⎥ ⎢⎥

(^010) ⎡⎤ ⎢⎥ (^101) ⎢⎥ ⎢⎥ (^3 12) ⎢⎥ (^1101) ⎢ ⎥^1110 ⎣⎦

2 ⎢^ ⎥^0 0 0 ⎣⎦^ G^2

G^1

G^2

(^40512)

1 2 3

3

(^67)

7

2 G 1

0 2 G^3

6 G^4

G^3

G^4

An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

Graph Traversalp Problem:^ Search for a certain node or traverseProblem:^ Search for a certain node or traverse all nodes in the graph Depth First SearchDepth First Search^ Once a possible path is found, continue the searchuntil the end of the pathuntil the end of the path Breadth First SearchSt^ t^

l^ th^ t^ ti

d^ d^

i^ h

Start several paths at a time, and advance in eachone step at a time

Depth-First SearchDepth First Search

B^ A C^ D B^ A C^ D F^ E G^ H I^ J^

K^ L M^ N^

O^ P M^ N^

O^ P

docsity.com

More BFSMore BFS 0 1 2

3 0

1 2

3

B^ A C^ D^

A^ B^

C^ D

E^ F^

G^ H^4

E^ F^

G^ H^4

J^ I K^ L^

I^ J^

K^ L

M^ N^

O^ P^

M^ N^

O^ P^5

BFS Pseudo-Code Algorithm BFS(s): Input: A vertex s in a graphOutput: A labeling of the edges as “discovery” edges and “cross edges”i iti li^ t i^ L

t^ t i^

t initialize container L

to contain vertex s 0 i^ ←^^0 while L^ is not empty doi^ create container L

to initially be emptyi+ for each vertex

v^ in Ldoi^ if edge^ e^ incident on v dog let^ w^ be the other endpoint of

e if vertex w is unexplored thenlabel^ e^ as a discovery edgelabel^ e^ as a discovery edgeinsert^ w^ into Li+1else label e as a cross edgei i

26

i^ ←^ i^ + 1

Applications: Finding a Pathpp^

g

Find path from source vertex s to destinationdvertex d Use graph search starting at s and terminating as soon as we reach d^ Need to remember edges traversed Use depth – first search? Use breath – first search?Use breath

first search?

DFS vs. BFS^ E

F^ B^

A^ start

DFS Process

G^ DC destinationDFS on CC DFS on BBB

Call DFS on DD Return to call on BB

DFS on A^ A A A

A C ll^ S^ G^

f^ d d^ ti^

ti^ d^! Call DFS on GG (^) D B

found destination - done!Path is implicitly stored in DFS recursionPath is: A, B, D, G B^ A

Path is: A, B, D, G

docsity.com

DFS vs. BFS^ E

FB GD

A^ start

BFS Process^

G^ DC destinationfront rear^

frontrear

frontrear

frontrear

A Initial call to BFS on A

B Dequeue A frontrear

frontrear

frontrearCD (^) Dequeue B

frontrearD Dequeue C

Initial call to BFS on AAdd A to queue

Dequeue AAdd B

Dequeue BAdd C, D

Dequeue CNothing to add

frontrear Gfound destination - done!Dequeue DPath must be stored separatelyAdd G Add G

Referencef Rada MihalceaRada Mihalcea John Lewis and William Loftus Data structure through C in depthData structure through C in depth Data Structure through C

docsity.com