Ch12 Graphs
Ch12 Graphs
Chapter 12
Graphs
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
V(G4)={1,2,3,4,5}
E(G4)={(1,2),(1,3) G4
,(1,4),(2,5), (3,4), (3,5)}
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
Undirected Graph Definitions and Notations
• Let u and v be two vertices in the undirected graph G
– u and v adjacent
• If edge from one to the other exists: (u, v) E
– A and B are adjacent
A B C
• Loop
– Edge incident on a single vertex
• e1 and e2 called parallel edges D
6
Undirected Graph Definitions and Notations
D
• 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
• There is a path from A to D but not from D to A
• B-C-D-B is a cycle
9
directed Graph Definitions and
Notations
• G is strongly connected, or simply strong, if it contains a directed path
from u to v and a directed path from v to u for every pair of vertices u, v.
• G is weakly connected if replacing all of its directed edges with undirected
edges produces a connected (undirected) graph
A B C A B C
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 > 0
• Let V(G) = {v1, v2, ..., vn}
– Adjacency matrix
12
Adjacency Matrices: Undirected Graphs
13
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
14
Adjacency Lists (cont’d.)
17
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
18
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
20
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
21
Breadth First Traversal (cont’d.)
• General search algorithm
– Breadth first search algorithm with a queue
A C F
B D E
G
H
A C D E G F B H
22
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
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
31