GRAPHS
Outline
• Graph- Concept
• Graph terminology: vertex, edge, adjacent, incident,
degree, cycle, path, connected component, spanning tree
• Types of graphs: undirected, directed, weighted
• Graph representations: adjacency matrix, array adjacency
lists, linked adjacency lists
• Graph search methods: breath-first, depth-first search
What is a graph?
• A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the
nodes to each other
• The set of edges describes relationships
among the vertices
Formal definition of graphs
• A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
• Vertices are also called nodes and points.
• Each edge connects two vertices.
• Edges are also called arcs and lines.
• Vertices i and j are adjacent vertices iff (i, j) is an edge in
the graph
• The edge (i, j) is incident on the vertices i and j
Graphs
• Undirected edge has no orientation (no arrow head)
• Directed edge has an orientation (has an arrow head)
• Undirected graph – all edges are undirected
• Directed graph – all edges are directed
u v u v
undirected edge directed edge
Undirected Graph
Directed Graph (Digraph)
Directed vs. undirected graphs
• When the edges in a graph have no direction, the
graph is called undirected
Directed vs. undirected graphs (cont.)
• When the edges in a graph have a direction, the graph
is called directed (or digraph)
Warning: if the graph is directe
d, the order of the vertices in ea
ch edge is important !!
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)
Trees vs graphs
• Trees are special cases of graphs!!
Graph terminology
• Adjacent nodes: two nodes are adjacent if they are
connected by an edge
5 is adjacent to 7
7 is adjacent from 55
• Path: a sequence of vertices that connect two nodes in
a graph
• Complete graph: a graph in which every vertex is
directly connected to every other vertex
Graph terminology (cont.)
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
2
O( N )
Graph terminology (cont.)
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Graph terminology (cont.)
• Weighted graph: a graph in which each edge
carries a value
Applications – Communication Network
vertex = router
edge = communication link
Applications - Driving Distance/Time Map
vertex = city
edge weight = driving distance/time
Applications - Street Map
• Streets are one- or two-way.
• A single directed edge denotes a one-way street
• A two directed edge denotes a two-way street
• Read Example 16.1 and see Figure 16.2
Path
• A sequence of vertices P = i1, i2, …, ik is an i1 to ik path in
the graph G=(V, E) iff the edge (ij, ij+1) is in E for every j,
1≤ j < k
• What are possible paths in Figure 16.2(b)?
Simple Path
• A simple path is a path in which all vertices, except
possibly in the first and last, are different
Length (Cost) of a Path
• Each edge in a graph may have an associated length (or
cost). The length of a path is the sum of the lengths of the
edges on the path
• What is the length of the path 5, 9, 11, 10?
Subgraph & Cycle
• Let G = (V, E) be an undirected graph
• A graph H is a subgraph of graph G iff its vertex and
edge sets are subsets of those of G
• A cycle is a simple path with the same start and end
vertex
• List all cycles of the graph of Figure 16.1(a)?
• 1, 2, 3, 1
• 1, 4, 3, 1
• 1, 2, 3, 4, 1
Graph Properties
Number of Edges – Undirected Graph
• Each edge is of the form (u,v), u != v.
• The no. of possible pairs in an n vertex graph is n*(n-1)
• Since edge (u,v) is the same as edge (v,u), the number of
edges in an undirected graph is n*(n-1)/2
• Thus, the number of edges in an undirected graph
is n*(n-1)/2
Number of Edges - Directed Graph
• Each edge is of the form (u,v), u != v.
• The no. of possible pairs in an n vertex graph is n*(n-1)
• Since edge (u,v) is not the same as edge (v,u), the number
of edges in a directed graph is n*(n-1)
• Thus, the number of edges in a directed graph is n*(n-1)
Vertex Degree
• The degree of vertex i is the no. of edges incident on
vertex i.
e.g., degree(2) = 2, degree(5) = 3, degree(3) = 1
In-Degree of a Vertex
• In-degree of vertex i is the number of edges incident to i
(i.e., the number of incoming edges).
e.g., indegree(2) = 1, indegree(8) = 0
Out-Degree of a Vertex
• Out-degree of vertex i is the number of edges incident from i
(i.e., the number of outgoing edges).
• e.g., outdegree(2) = 1, outdegree(8) = 2
Complete Undirected Graphs
• A complete undirected graph has n(n-1)/2 edges (i.e., all possible edges)
and is denoted by Kn
What would a complete undirected graph look like when
n=5? When n=6?
Connected Graph
• Let G = (V, E) be an undirected graph
• G is connected iff there is a path between every pair of
vertices in G
Example of Not Connected
Example of Connected Graph
Representation of Unweighted Graphs
• The most frequently used representations for unweighted
graphs are
• Adjacency Matrix
• Linked adjacency lists
• Array adjacency lists
Adjacency Matrix
• 0/1 n x n matrix, where n = # of vertices
• A(i, j) = 1 iff (i, j) is an edge.
Adjacency Matrix Properties
• Diagonal entries are zero.
• Adjacency matrix of an undirected graph is symmetric
(A(i,j) = A(j,i) for all i and j).
Adjacency Matrix for Digraph
• Diagonal entries are zero(only if there is no self loop)
• Adjacency matrix of a digraph need not be symmetric.
Adjacency Lists
• Adjacency list for vertex i is a linear list of vertices
adjacent from vertex i.
• An array of n adjacency lists for each vertex of the graph.
Linked Adjacency Lists
• Each adjacency list is a chain.
Array length = n.
# of chain nodes = 2e (undirected graph)
# of chain nodes = e (digraph)
Array Adjacency Lists
• Each adjacency list is an array list.
Array length = n.
# of chain nodes = 2e (undirected graph)
# of chain nodes = e (digraph)
Representation of Weighted Graphs
• Weighted graphs are represented with simple extensions
of those used for unweighted graphs
• The cost-adjacency-matrix representation uses a matrix
C just like the adjacency-matrix representation does
• Cost-adjacency matrix: C(i, j) = cost of edge (i, j)
• Adjacency lists: each list element is a pair
(adjacent vertex, edge weight)
For the digraph Figure 16.2(b)
(a) adjacency matrix (b) Linked adjacency list
(c) Array adjacency list
41
Graph Traversals (Search)
• We have covered some of these with binary trees
• Breadth-first search (BFS)
• Depth-first search (DFS)
• A traversal (search):
• An algorithm for systematically exploring a graph
• Visiting (all) vertices
• Until finding a goal vertex or until no more vertices
Only for connected graphs
42
Breadth-first search
• One of the simplest algorithms
• Also one of the most important
• It forms the basis for MANY graph algorithms
43
BFS: Level-by-level traversal
• Given a starting vertex s
• Visit all vertices at increasing distance from s
• Visit all vertices at distance k from s
• Then visit all vertices at distance k+1 from s
• Then ….
44
BFS in a tree
BFS: visit all siblings before their descendants
ABCDEFGHIJKLM
45
BFS: Graph
A
B E
G C D
ABEGCDF
46
BFS(graph g, vertex s)
1. unmark all vertices in G
2. q new queue
3. mark s // s is starting vertex
4. enqueue(q, s)
5. while (not empty(q))
6. curr dequeue(q)
7. visit curr // e.g., print its data
8. for each edge <curr, V>
9. if V is unmarked
10. mark V
11. enqueue(q, V)
BFS algorithm
Starting vertex = d
49
Interesting features of BFS
• Complexity: O(|V| + |E|)
• All vertices put on queue exactly once
• For each vertex on queue, we expand its edges
• In other words, we traverse all edges once
• BFS finds shortest path from s to each vertex
• Shortest in terms of number of edges
• Why does this work?
• Takes too much memory.
• Runs out of memory before it runs out of time.
53
Depth-first search
• Again, a simple and powerful algorithm
• Given a starting vertex s
• Pick an adjacent vertex, visit it.
• Then visit one of its adjacent vertices
• …..
• Until impossible, then backtrack, visit another
54
DFS(graph g, vertex s)
1. unmark all vertices in G
2. Stack new stack
3. Push(stack, s)
4. while (not empty(stack))
5. curr pop(stack)
6. If not marked curr
7. visit curr // e.g., print its data
8. Mark curr
9. for each edge <curr, V>
10. if V is unmarked
11. push(stack, V)
55
Current vertex: A
A
B E
G C D
Start with A. Mark it.
56
Current: B
A
B E
G C D
Expand A’s adjacent vertices. Pick one (B).
Mark it and re-visit.
57
Current: C
A
B E
G C D
Now expand B, and visit its neighbor, C.
58
Current: F
A
B E
G C D
Visit F.
Pick one of its neighbors, E.
59
Current: E
A
B E
G C D
E’s adjacent vertices are A, D and F.
A and F are marked, so pick D.
60
Current: D
A
B E
G C D
Visit D. No new vertices available. Backtrack to
E. Backtrack to F. Backtrack to C. Backtrack to B
61
Current: G
A
B E
G C D
Visit G. No new vertices from here. Backtrack to
B. Backtrack to A. E already marked so no new.
62
Current:
1
A
2
5
B E
3 6
G 7 C D
4
F
Done. We have explored the graph in order:
ABCFEDG
Method 1
Method 2
65
Interesting features of DFS
• Complexity: O(|V| + |E|)
• All vertices visited once, then marked
• For each vertex on stack, we examine all edges
• In other words, we traverse all edges once
• DFS does not necessarily find shortest path
• Why?
• Not a good choice when the goal node is at shallow level
on right side of the graph