Vous êtes sur la page 1sur 64

Data Structure & Algorithms

Chapter 7:
Graph
Imran Ali Memon
Lecturer IT Department

A
B C

D E

F
G
Linear Data Structures
Non-Linear Data Structure
7.1 Introduction
• A graph is a non-linear data structure.
• Graph is a collection of nodes V connected by edges E.
– Nodes also called vertices, singular is vertex
– Edges also called arcs
– Each node contains an element
– Each edge connects two nodes together (or possibly the same node
to itself) and may contain an edge attribute

Mathematically :
G=(V,E)  (G is an ordered pair of V and E)
V(G): set of vertices
E(G): a set of edges
7.2 Types of Graphs
• There are two kinds of graphs:
1. Directed graphs (Unidirectional)
– Sometimes called digraphs
– Edges have a direction
– Warning: if the graph is directed, the
order of the vertices in each edge is
important !!

2. Undirected graphs (Bidirectional)


– Edges have no direction
– Order of vertices isn’t important
Terminology: Weighted & Unweighted Graph
• A graph in which each edge
carries a value is called
weighted graph

Unweighted
Graph

Weighted
Graph
Terminology: Labeled & Unlabeled Graph
• In labeled graph each
vertex is assigned a unique
name or identifier to
distinguish it from all other
vertices Unlabeled
Graph

Labeled
Graph
Terminology: Trees vs Graphs
• Trees are special cases of graphs!!
• Tree
– If N nodes then N-1 edges.
– 1 edge for each parent-child
– All nodes must be reachable from root
• Graph
– No rules for connection among nodes.
– Edges can connect nodes in any possible way a
a
e
b e
c

f b d
f d
Terminology: Complete Graph
b
 A complete graph is a graph that has
the maximum number of edges a d
– for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2 c
– 4(4-1)/2 = 4(3)/2 = 12/2 = 6

b
– for directed graph with n vertices, the
maximum
number of edges is n(n-1) a d
– 4(4-1) = 4(3) = 12
c
Terminology: Node(Vertex) & Edge (Arc)
Nodes

• Node is a data element in graph


– Vertices set V(G) = {A, B, C, D, E, F, G} A
B
E
D F
• Edge is a line that connects two C
nodes. G
– Edge set E(G) = {{A,D}, {A,E}, {D,G},
{G,C}, {E,F}, {E,B}, {B,C}} Undirected Graph
– For undirected graph {A,D} = {D,A} Edges
– For directed graph {A,D} != {D,A}
Terminology: Size, Neighbors/Adjacent Nodes,
Endpoints
A
• Size is number of nodes in B
graph. E
– Size of this graph is 7 D F
– 0 node = empty graph C
G
Undirected Graph

• {A,D} {A,E} {E,B} {E,F} {D,G}


{G,C} {C,B}are neighbor nodes.
– connected by same edge D A

D is adjacent of A
D and A are endpoints of edge A is adjacent of D
Terminology: Degree (In-Deg & Out-Deg)
A
• The degree of a node is the number of B
edges it has. E
D F
– deg(E) = 3, deg(F) = 2 …
C
G
• For directed graphs, Undirected Graph

– The in-degree of a node is the number of in-


A
edges it has B
• Indeg(E) = 2 , Indeg(F) = 0 E
– The out-degree of a node is the number of D
out-edges it has F
• Outdeg(A)=2 , Outdeg(B) = 0 G C

Directed Graph
Terminology: Source , Sink
• A node is called a source if it has A
B
outdegree but zero indegree.
E
– A & F are sources vertices
D
F

• A node is called a sink if it has G C

indegree but zero outdegree. Directed Graph


– B is sink vertex
Terminology:
• If a directed edge goes from node A to D, we call
– A the initial point/ origin and D the destination/
terminal point of the edge
– The edge is an out-edge of A and an in-edge of D
– A is a predecessor of D, and D is a successor of A
A
B
E
D
F
G C

Directed Graph
Terminology: Isolated node
• If only one node in graph then, it is
called as isolated node. A
OR
• If deg(node) = 0 , it is called as
isolated node.
Terminology: Path A
E

• A sequence of vertices where each D


C

adjacent node is connected by an F


G
edge.
The path P is said to be
• < A, D, G, C > simple if all nodes are
different.

A
E

D
The path P is said to be closed path if first nodes is last node.
< A, D, G, C, A > G
F

First node is repeated , so it isn’t simple path.


C
Terminology: Cycle, K-cycle
• Cycle is a closed path with length 3
or more.
A
• Number of nodes K in cycle is E
called k-cycle.
D
• < A, D, G, C, A > is cycle 4.
F
G

C
Terminology: Multiedges, Loop
If a graph contains multiedges or loop , then it is called
multigraph.
• If different edges connect the
f
same endpoints is called multi
edges. a
A E
– e = {A,D} , d = {A,D} e
d b

D F
• If an edge has identical endpoints c

is called loop.
– f = {E,E}
Exercise of terminologies
• Graph
• Multigraph
f
• loop f={E,E}
a
• multiedges = d={A,D} & e={A,D} A E
• 4 nodes / vertices e
d b

• 6 edges / arcs D F
c
• …
Traversing a Graph
• How do we search a graph?
– At a particular vertex, where shall we go next?
• There are two standard ways:
1. Breadth first search
– Use a queue, to hold nodes for future processing
2. Depth first search
– Use a stack, to hold nodes for future processing
Breadth First Search
• Explore a graph level by level.
• The general idea behind a BFS is beginning at a
starting node A.
• 1. First we examine the starting node A.
• 2. Then we examine all the neighbors of A.
• 3. Then we examine all neighbors of neighbors of
A.
• And so on…
Breadth First Search (Cont…)
• We need to keep track if the neighbors of a node.
• We need to guarantee that no node is processed
more than once.
• This is accomplished by using a queue to hold
nodes that are waiting to be procesed.
An Example of BFS
A B C D

E F G H

I J K L

M N O P
Data Structure and Algorithm
0 A B C D

E F G H

I J K L

M N O P
Data Structure and Algorithm
0 A B C D
1

1 E F 1 G H

I J K L

M N O P
Data Structure and Algorithm
0 A B C D
1 2

1 E F 1 G H

2 I J K L

M N O P
Data Structure and Algorithm
0 A B C D 3
1 2

1 E F 1 G 3 H

2 I J K L

3 M N 3 O P
Data Structure and Algorithm
0 A B C D 3
1 2

1 E F 1 G 3 H 4

2 I J K 4 L 4

3 M N 3 O P
Data Structure and Algorithm
0 A B C D 3
1 2

1 E F 1 G 3 H 4

2 I J K 4 L 4

5
3 M N 3 O P 5
Data Structure and Algorithm
0 A B C D 3
1 2

1 E F 1 G 3 H 4

2 I J K 4 L 4

5
3 M N 3 O P 5
Data Structure and Algorithm
0 A B C D 3
1 2

1 E F 1 G 3 H 4

2 I J K 4 L 4

5
3 M N 3 O P 5
Data Structure and Algorithm
BFS Example
BFS Example
r s t u

   

   
v w x y

Data Structure and Algorithm


BFS Example
r s t u

 0  

   
v w x y

Q: s
Data Structure and Algorithm
BFS Example
r s t u

1 0  

 1  
v w x y

Q: w r
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 

 1 2 
v w x y

Q: r t x
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 

2 1 2 
v w x y

Q: t x v
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 3

2 1 2 
v w x y

Q: x v u
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
Data Structure and Algorithm
BFS Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
Data Structure and Algorithm
Depth First Search
• Go as far as possible along a single path until reach a
dead end, then backtrack.
• The general idea behind a DFS beginning at a starting
node A.
• First we examine the starting node A.
• Then we examine each node along a path which begins
at A.
• After coming to a dead end that is the end of the path,
we backtrack on path until we continue along another
path and so on…
Depth First Search
• The graph below, started at node a, would be visited in the following
order: a, b, c, g, h, i, e, d, f, j
a c d
b
e f

g h i j

J
F
D
E
C G H I I
B B B B B H
A B
A A A A A A
DFS Example
source
vertex

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

| |

| | |

d: discover time
Data Structure and Algorithm f: finish time
DFS Example
source
vertex
d f
1 | | |

2 | |

| | |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

2 | |

3 | | |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 | |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | | |

2 | 7 |

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |10

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 | 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 |12 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 |

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|15

Data Structure and Algorithm


DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Data Structure and Algorithm


7.4 Shortest Path Problem
• Given a weighted graph and two vertices u and v, we want to find a path of
minimum total weight between u and v.
– Length of a path is the sum of the weights of its edges.
• Example:
– Shortest path between City A to City F
• Applications
– Internet packet routing
– Flight reservations
– Driving directions
City F
City E
City B
City G

City A
City C
City D
City H
Shortest Paths 66

Vous aimerez peut-être aussi