Chap 6-Graph
Chap 6-Graph
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
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
D D
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.)
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
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.
r s t u
0
v w x y
Q: s
0
r s t u
1 0
1
v w x y
Q: w r
1 1
r s t u
1 0 2
1 2
v w x y
Q: r t x
1 2 2
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
2 2 2
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
2 2 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
2 3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q:
r s t u
1 0 2 3
2 1 2 3
v w x y
BF Tree
u v w
1/
x y z
u v w
1/ 2/
x y z
u v w
1/ 2/
3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
4/5 3/
x y z
u v w
1/ 2/
4/5 3/6
x y z
u v w
1/ 2/7
4/5 3/6
x y z
u v w
1/ 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/12
F B C
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