Graphs
Graphs
Analysis
Semester 2, Session 2017/18
Lecture 7: Graph
Learning objectives
• Terminology
• Acyclic graph
• Graphs Representation
• Connected components
• Graph Algorithm
Elementary : BFS, DFS, Topological Sort
Minimum Spanning Tree : Kruskal and Prims
Single Source Shortest Path : Bellman-Ford, Djikstra’s Algorithm
All Pair Shortest Path : Floyd-Warshall, Johnson Algorithm
Graph Applications
• Modeling connectivity in computer networks
• Representing maps
• Modeling flow capacities in networks
• Finding paths from start to goal (AI)
• Modeling transitions in algorithms
• Ordering tasks
• Modeling relationships (families, organizations)
RJRY 3
Terminology
A graph G = (V, E) consists of a set of vertices V, and a set of
edges E, such that each edge in E is a connection between a
pair of vertices in V.
The number of vertices is written |V|, and the number edges
is written |E|.
Two vertices are called neighbors if they are adjacent to one
another and joined by an edge
An edge connecting vertices u and v is written (u,v)
• It is said to be incident on vertices u and v
• Can also associate each edge with cost or weight like in previous figure
c)
WKES3311 RJRY 4
Acyclic Graph
• Acyclic Graph: a graph without cycles
• A directed graph without cycles is called a directed acyclic
graph or DAG
• A free tree is a connected, undirected graph with no simple
cylce: connected with |V|-1 edges
RJRY 5
Methods for representing graphs
• Two methods:
• Directed representation
• Undirected representation
• Both method involves:
• The adjacency matrix (|V|X|V| array)
• The adjacency list
RJRY 6
Graphs Representation
RJRY 8
Directed Representation
RJRY 9
Undirected Representation
RJRY 10
Representation Costs
Adjacency Matrix:(|V2|)
- since it requires space for each potential edge
Adjacency List: (|V|+|E|)
- since there must be an array entry for each vertex and
- each edge must at least appear in one of the vertex
RJRY 11
Elementary - Depth First Search (1)
RJRY 12
Elementary - Depth First Search (2)
RJRY 14
Elementary - Breadth First Search (2)
RJRY 15
Elementary - Topological Sort (1)
The following simple algorithm topologically sorts
1. call DFS.G/ to compute finishing times :f for each
vertex
2. as each vertex is finished, insert it onto the front of
a linked list
3. return the linked list of vertices
RJRY 16
Elementary - Topological Sort (2)
17
Minimal Cost Spanning Trees
Minimal Cost Spanning Tree (MST) Problem:
Input: An undirected, connected graph G.
Output: The subgraph of G that
1) has minimum total cost as measured by summing the values of
all the edges in the subset, and
2) keeps the vertices connected.
RJRY 18
MST - Kruskal’s Algorithm (1)
Initially, each vertex is in its own MST.
Merge two MST’s that have the shortest edge between
them.
• Use a priority queue to order the unprocessed edges. Grab next
one at each step.
RJRY 19
MST - Kruskal’s Algorithm (2)
MST - Kruskal’s Algorithm (3)
RJRY 21
MST - Kruskal’s Algorithm (4)
Cost is dominated by the time to remove edges from the
heap.
• Can stop processing edges once all vertices are in the same
MST
RJRY 22
MST - Prim’s Algorithm (1)
RJRY 23
MST - Prim’s Algorithm (2)
MST - Prim’s Algorithm (3)
i ii iii
MST - Prim’s Algorithm (4)
iv v
Shortest Paths Problems
Input: A graph with weights or costs associated with each
edge.
Output: The list of edges forming the shortest path.
Sample problems:
• Find shortest path between two named vertices
• Find shortest path from S to all other vertices
• Find shortest path between all pairs of vertices
Will actually calculate only distances.
RJRY 27
Shortest Paths Definitions
d(A, B) is the shortest distance from
vertex A to B.
w(A, B) is the weight of the edge
connecting A to B.
• If there is no such edge, then w(A, B) = .
RJRY 28
Single-Source Shortest Paths
Given start vertex s, find the shortest path from s
to all other vertices.
Try 1: Visit vertices in some order, compute
shortest paths for all vertices seen so far, then
add shortest path to next vertex x.
Problem: Shortest path to a vertex already
processed might go through x.
Solution: Process vertices in order of distance from
s.
RJRY 29
Single Source Shortest Path -
Dijkstra’s Algorithm (1)
A B C D E
Initial 0
Process A 0 10 3 20
Process C 0 5 3 20 18
Process B 0 5 3 10 18
Process D 0 5 3 10 18
Process E 0 5 3 10 18
RJRY 30
Single Source Shortest Path - Dijkstra’s
Algorithm (2)
RJRY 31
Single Source Shortest Path -
Dijkstra’s Algorithm (3)
Shortest Path – Bellman-Ford
Algorithm (1)
Single Source Shortest Path – Bellman-
Ford Algorithm (2)
All-Pairs Shortest Paths
For every vertex u, v V, calculate d(u, v).
Could run Dijkstra’s Algorithm |V| times.
Better is Floyd’s Algorithm.
Define a k-path from u to v to be any path whose
intermediate vertices all have indices less than k.
RJRY 35
All-Pairs Shortest Paths - Floyd’s
Algorithm (1)
RJRY 36
All-Pairs Shortest Paths - Floyd’s
Algorithm (2)
RJRY 37
All-Pairs Shortest Paths - Floyd’s
Algorithm (3)
RJRY 38
All-Pairs Shortest Paths - Johnson
Algorithm (1)
RJRY 39
All-Pairs Shortest Paths - Johnson
Algorithm (2)
RJRY 40
All-Pairs Shortest Paths - Johnson
Algorithm (3)
RJRY 41