Graph
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
• Example:
a b V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
c (b,e),(c,d),(c,e),
(d,e)}
d e
Applications
CS16
• electronic circuits
JFK
LAX STL
HNL
DFW
FTL
Terminology:
Adjacent and Incident
• If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
3 1 2 3 3 4 5 6
1 1 1
3 1
3 G2
G1 0 in:1, out: 1
directed graph
1 in: 1, out: 2
in-degree
out-degree
2 in: 1, out: 0
G3
Terminology:
Path
Path: 3 2
a b a b
c c
d e d e
abedc bedc
7
More Terminology
• simple path: no repeated vertices
a b
bec
c
d e
• cycle: simple path, except that the last vertex is the same as the first
vertex
a b
acda
c
d e
Even More Terminology
•connected graph: any two vertices are connected by some path
0 0 0 0 0
1 1 1
1
2 2
2 (i) (ii) (iii) (iv)
G3 (b) Some of the subgraph of G3
More…
• tree - connected graph without cycles
tree
forest
tree
tree
Connectivity
• Let n = #vertices, and m = #edges
n=5
m = (5 * 4)/2 = 10
More Connectivity
n = #vertices n=5
m = #edges
m=4
• For a tree m = n - 1
n=5
m=3
Oriented (Directed) Graph
tail head
ADT for Graph
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
3 1 3 6
é0 1 1 1ù
é0 1 0ù 7
ê1 0 1 1 úú 2 ê ú
ê 1 0 1 é0 1 1 0 0 0 0 0ù
ê ú ê1
ê1 1 0 1ú êë0 0 0úû ê 0 0 1 0 0 0 0úú
ê ú ê1 0 0 1 0 0 0 0ú
ë1 1 1 0û
ê ú
G2
G1 ê0 1 1 0 0 0 0 0ú
ê0 0 0 0 0 1 0 0ú
ê ú
ê0 0 0 0 1 0 1 0ú
symmetric ê0 0 0 0 0 1 0 1ú
ê ú
êë0 0 0 0 0 0 1 0úû
G4
Merits of Adjacency Matrix
From the adjacency matrix, to determine the
connection of vertices is easy
n -1
n -1 n -1
ind (vi ) = å A[ j , i ] outd (vi ) = å A[i , j ]
j =0 j =0
Adjacency Lists (data structures)
Each row in adjacency matrix is represented as an adjacency list.
#define MAX_VERTICES 50
typedef struct node *node_pointer;
node_pointer graph[MAX_VERTICES];
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4 An undirected graph with n
2 vertices and e edges ==> n
head nodes and 2e list nodes
Some Operations
!degree of a vertex in an undirected graph
–# of nodes in adjacency list
!out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
!in-degree of a vertex in a directed graph
–traverse the whole data structure.
!# of edges in a graph
–determined in O(n+e)
Graph Traversal Techniques
traversal techniques
to the tree
DFS Tree
Implementation of DFS
Observations:
proceed.
BFS tree being formed. Its level is called the current level.
which the level nodes were visited, visit all the unvisited
0 1
0
2 4 2
1
9 4
5 9 10
10 5 7
11 8
6 7 8 11
6
BFS Tree Graph G
Implementation of DFS
Observations:
• We will redo the BFS on the previous graph, but this time with
queues
• In Class
BFS (Pseudo Code)
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){
visit(y); Enqueue(y,Q);
}
}
}
}