0% found this document useful (0 votes)
19 views70 pages

Chap 6-Graph

The document discusses graphs and graph algorithms. It defines graphs and common graph terminology like vertices, edges, paths, cycles, connectedness. It describes two common ways to represent graphs in memory: adjacency matrices and adjacency lists. It then explains two classic graph traversal algorithms - depth-first search and breadth-first search. Depth-first search recursively explores vertices like a depth-first tree traversal but uses a visited array to avoid cycles. Breadth-first search uses a queue to explore the neighbors of each vertex level-by-level to avoid cycles.

Uploaded by

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

Chap 6-Graph

The document discusses graphs and graph algorithms. It defines graphs and common graph terminology like vertices, edges, paths, cycles, connectedness. It describes two common ways to represent graphs in memory: adjacency matrices and adjacency lists. It then explains two classic graph traversal algorithms - depth-first search and breadth-first search. Depth-first search recursively explores vertices like a depth-first tree traversal but uses a visited array to avoid cycles. Breadth-first search uses a queue to explore the neighbors of each vertex level-by-level to avoid cycles.

Uploaded by

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

Data Structures Using C++ 2E

Chapter 13
Graphs
Edited by Malak Abdullah
Jordan University of Science and Technology
Objectives
• Learn about graphs
• Become familiar with the basic terminology of
graph theory
• Discover how to represent graphs in computer
memory
• Examine and implement various graph traversal
algorithms

2
Graph Definitions and Notations
• Graph G pair
– G = (V, E), where
• V is a finite nonempty set
– Called the set of vertices of G, and E  V x V
• Elements of E
– Pairs of elements of V
• E: set of edges of G
• G called trivial if it has only one vertex
• Directed graph (digraph)
– Elements in set of edges of graph G: ordered
• Undirected graph: not ordered
3
FIGURE 12-3 Various undirected graphs

FIGURE 12-4 Various directed graphs

4
Graph Definitions and Notations
• Graph H called subgraph of G
– If V(H)  V(G) and E(H)  E(G)
– Every vertex of H: vertex of G
– Every edge in H: edge in G
• Graph shown pictorially
– Vertices drawn as circles
• Label inside circle represents vertex
• Undirected graph: edges drawn using lines
• Directed graph: edges drawn using arrows

5
Graph Definitions and Notations
• Let u and v be two vertices in G A B C

– u and v adjacent
• If edge from one to the other exists: (u, v)  E D
• Loop
– Edge incident on a single vertex
• e1 and e2 called parallel edges
– edges e1 and e2 associate with same pair of vertices {u, v}
• Simple graph
A B C
– No loops, no parallel edges

6
Graph Definitions and Notations
• Let e = (u, v) be an edge in G
– Edge e is incident on the vertices u and v
– Degree of u written deg(u) or d(u)
• Number of edges incident with u A B C
• Each loop on vertex u
– Contributes two to the degree of u
D
• u is called an even (odd) degree vertex
– If the degree of u is even (odd)

7
Graph Definitions and Notations
• Path from u to v
– If sequence of vertices u1, u2, . . ., un exists
• Such that u = u1, un = v and (ui, ui+ 1) is an edge for all i =1,
2, . . ., n – 1
A B C
• Vertices u and v called connected
– If path from u to v exists
• Simple path D

– All vertices distinct (except possibly first, last)


– A to C / A to B / A to D/ D to C/ D to B/ B to C/ B to D/ C to B / C to D
– There is no path to A
• Cycle in G
– Simple path in which first and last vertices are the same
– A Cycle such as B C D B
8
Graph Definitions and Notations
• G is connected
– If path from any vertex to any other vertex exists
A B C A B C

D D

• Let G be a directed graph and let u and v be two vertices


in G
– If edge from u to v exists: (u, v)  E
• u is adjacent to v
• v is adjacent from u
» A adjacent to B but B adjacent from A

9
Graph Definitions and Notations
• Definitions of paths and cycles in G
– Similar to those for undirected graphs
• G is strongly connected
– If any two vertices in G are connected

10
Graph Representation
• Graphs represented in computer memory
– Two common ways
• Adjacency matrices
• Adjacency lists

11
Adjacency Matrices
• Let G be a graph with n vertices where n > zero
• Let V(G) = {v1, v2, ..., vn}
– Adjacency matrix

12
Adjacency Lists
• Given:
– Graph G with n vertices, where n > zero
– V(G) = {v1, v2, ..., vn}
• For each vertex v: linked list exists
– Linked list node contains vertex u: (v, u)  E(G)
• Use array A, of size n, such that A[i]
– Reference variable pointing to first linked list node
containing vertices to which vi adjacent
• Each node has two components: vertex, link
– Component vertex
• Contains index of vertex adjacent to vertex i
13
Adjacency Lists (cont’d.)

FIGURE 12-4 Various directed graphs

Adjacency list of graph G2 Adjacency list of graph G3


14
Graph Traversals
– Similar to traversing a binary tree
• A bit more complicated
• Two most common graph traversal algorithms
– Depth first traversal
– Breadth first traversal

15
Depth First Traversal
• Similar to binary tree preorder traversal
• General algorithm

A B
A C F
C D E A D H

B D E E G F

F
H G

A C E F G D B H

16
Depth First Traversal (cont’d.)
• General algorithm for depth first traversal at a given
node v
– Recursive algorithm

Data Structures Using C++ 2E 17


Depth First Traversal (or Search) for a graph is
similar to Depth First Traversal of a tree. The only
catch here is, unlike trees, graphs may contain
cycles, so we may come to the same node again.
To avoid processing a node more than once, we
use a boolean visited array.
For example, in the following graph, we start traversal
from vertex 2. When we come to vertex 0, we look for all
adjacent vertices of it. 2 is also an adjacent vertex of 0. If
we don’t mark visited vertices, then 2 will be processed
again and it will become a non-terminating process. A
Depth First Traversal of the following graph is 2, 0, 1, 3.

Edited by Malak Abdullah 18


Jordan University of Science and
Edited by Malak Abdullah 19
Jordan University of Science and
Edited by Malak Abdullah 20
Jordan University of Science and
Edited by Malak Abdullah 21
Jordan University of Science and
Edited by Malak Abdullah 22
Jordan University of Science and
Edited by Malak Abdullah 23
Jordan University of Science and
Edited by Malak Abdullah 24
Jordan University of Science and
Edited by Malak Abdullah 25
Jordan University of Science and
Breadth First Traversal
• Similar to traversing binary tree level-by-level
– Nodes at each level
• Visited from left to right
– All nodes at any level i
• Visited before visiting nodes at level i + one

A B
A C F

C D E A D H

B D E E G F

H G F

A C D E G F B H

26
Breadth First Traversal in Queue way

A C F

B D E
A C D E G F B H
G
H

A C D E G F B H

27
Breadth First Traversal (cont’d.)
• General search algorithm
– Breadth first search algorithm with a queue

28
Breadth First Traversal (or Search) for a graph is similar to
Breadth First Traversal of a tree (See method 2 of this post
). The only catch here is, unlike trees, graphs may contain
cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a boolean visited
array. For simplicity, it is assumed that all vertices are
reachable from the starting vertex.
For example, in the following graph, we start traversal from
vertex 2. When we come to vertex 0, we look for all adjacent
vertices of it. 2 is also an adjacent vertex of 0. If we don’t
mark visited vertices, then 2 will be processed again and it
will become a non-terminating process. A Breadth First
Traversal of the following graph is 2, 0, 3, 1.

Edited by Malak Abdullah 29


Jordan University of Science and
Edited by Malak Abdullah 30
Jordan University of Science and
Edited by Malak Abdullah 31
Jordan University of Science and
Edited by Malak Abdullah 32
Jordan University of Science and
Edited by Malak Abdullah 33
Jordan University of Science and
Edited by Malak Abdullah 34
Jordan University of Science and
Edited by Malak Abdullah 35
Jordan University of Science and
Example (BFS)
(Courtesy of Prof. Jim Anderson)

r s t u
 0  

   
v w x y

Q: s
0

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0  

 1  
v w x y

Q: w r
1 1

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 

 1 2 
v w x y

Q: r t x
1 2 2

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 

2 1 2 
v w x y

Q: t x v
2 2 2

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 
v w x y

Q: x v u
2 2 3

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: v u y
2 3 3

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: u y
3 3

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: y
3

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: 

Comp 122, Fall


2004
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

BF Tree

Comp 122, Fall


2004
Example (DFS)

u v w
1/

x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

3/
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

4/5 3/
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/7

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/ 2/7

F B

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7

F B

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/

F B

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6
x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/


x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/ B


x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/11 B


x y z

Comp 122, Fall


2004
Example (DFS)

u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y z

Comp 122, Fall


2004
Shortest Path (greedy algorithm)
• Weight of the graph
– Nonnegative real number assigned to the edges
connecting to vertices
• Weighted graphs
– When a graph uses the weight to represent the
distance between two places
• Weight of the path P
– Given G as a weighted graph with vertices u and v in
G and P as a path in G from u to v
• Sum of the weights of all the edges on the path
• Shortest path: path with the smallest weight

Data Structures Using C++ 2E 62


Shortest Path Algorithm (cont’d.)
• Shortest path algorithm space (greedy algorithm)
• See code on page 700
– class weightedGraphType
• Extend definition of class graphType
• Adds function createWeightedGraph to create
graph and weight matrix associated with the graph

Data Structures Using C++ 2E 63


Shortest Path
• General algorithm
– Initialize array smallestWeight
smallestWeight[u] = weights[vertex, u]
– Set smallestWeight[vertex] = zero
– Find vertex v closest to vertex where shortest path is
not determined
– Mark v as the (next) vertex for which the smallest
weight is found

Data Structures Using C++ 2E 64


Shortest Path (cont’d.)
• General algorithm (cont’d.)
– For each vertex w in G, such that the shortest path
from vertex to w has not been determined and an
edge (v, w) exists
• If weight of the path to w via v smaller than its current
weight
• Update weight of w to the weight of v + weight of edge
(v, w)

Data Structures Using C++ 2E 65


Shortest Path (cont’d.)
[0] [1] [2] [3] [4]
[0] 0 16 ∞ 2 3
[1] ∞ 0 5 ∞ ∞
[2] ∞ 3 0 ∞ ∞
[3] ∞ 12 ∞ 0 7
[4] ∞ 10 4 5 0
FIGURE 12-8 Weighted graph G w[5][5];

FIGURE 12-9 Graph after Steps 1 and 2 execute

Data Structures Using C++ 2E 66


Shortest Path (cont’d.)

FIGURE 12-10 Graph after the first iteration of Steps 3 to 5

FIGURE 12-11 Graph after the second iteration of Steps 3 to 5

Data Structures Using C++ 2E 67


Shortest Path (cont’d.)

FIGURE 12-12 Graph after the third iteration of Steps 3 to 5

FIGURE 12-13 Graph after the fourth iteration of Steps 3 through 5


Data Structures Using C++ 2E 68
Example
A
2 5  Depth First
3 ABCDE
E 6
B  Breadth First
10 5 ABCED
1
4 2  Shortest Path

D C
2
20
A C F  Depth First
10 ABDFCGE
8
12 20 4  Breadth First
5 D 3 E ABCDEGF
B 5
 Shortest Path
G

70

You might also like