0% found this document useful (0 votes)
76 views41 pages

Graphs

This document summarizes key concepts in graph algorithms. It discusses graph terminology, representations using adjacency matrices and lists, connected components, and elementary graph algorithms like breadth-first search, depth-first search, and topological sorting. It also covers minimum spanning tree algorithms like Kruskal's and Prim's, and single-source and all-pairs shortest path algorithms like Dijkstra's, Bellman-Ford, Floyd-Warshall, and Johnson's. The document provides pseudocode and examples to illustrate how each graph algorithm works.

Uploaded by

Nik Hakimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views41 pages

Graphs

This document summarizes key concepts in graph algorithms. It discusses graph terminology, representations using adjacency matrices and lists, connected components, and elementary graph algorithms like breadth-first search, depth-first search, and topological sorting. It also covers minimum spanning tree algorithms like Kruskal's and Prim's, and single-source and all-pairs shortest path algorithms like Dijkstra's, Bellman-Ford, Floyd-Warshall, and Johnson's. The document provides pseudocode and examples to illustrate how each graph algorithm works.

Uploaded by

Nik Hakimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

WIA2005: Algorithm Design and

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

a) edges not directed: undirected graph


b) Edges directed from one vertex to another: directed
graph
c) Labels associated with its vertices: labeled graph or
weighted graph
RJRY 7
Connected Components
An undirected graph is connected if there is at least one
path from any vertex to any other.
The maximum connected subgraphs of an undirected
graph are called connected components.

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)

Cost: (|V| + |E|).


RJRY 13
Elementary - Breadth First Search (1)
Like DFS, but replace stack with a queue.
• Visit vertex’s neighbors before continuing deeper in the tree.

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.

*USE GREEDY ALGORITHMS

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.

How to tell if an edge connects two vertices already in the


same MST?
• Use the UNION/FIND algorithm with parent-pointer
representation.

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

Total cost: (|V| + |E| log |E|).

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy