You are on page 1of 43

Week 03: Graph Algorithms CSE506

Week 3

Introduction (remaining portion)


+
Graph Algoriithms

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

How many shortest routes from A to B?

B
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 03: Graph Algorithms CSE506

How many shortest routes from A to B?

A route is any sequence containing 6 Ds and 8 Rs

14
C 6
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 03: Graph Algorithms CSE506

The number
of subsets of
an n-element
set is 2n .

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Lets use our principles to


extend our reasoning to
different types of objects.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

How many sequences of 7


letters are there?

26 7

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

How many sequences of 7


letters contain at least two
of the same letter?

267 - 26*25*24*23*22*21*20

Containing no double letter

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Sometimes it is easiest
to count something by
counting its opposite.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

How many ways to rearrange


the letters in the word
SYSTEMS?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506
SYSTEMS
1) 7 places to put the Y, 6 places to put the T, 5
places to put the E, 4 places to put the M, and the
Ss are forced.
7 X 6 X 5 X 4 = 840

7
2) C choices o f positions for the S's
3
4 choices for the Y
3 choices for the T
2 choices for the E
1 choice for the M
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 03: Graph Algorithms CSE506

SYSTEMS

7! 7!
4 321 = = 840
3!4 ! 3!

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506
SYSTEMS
3) Lets pretend that the Ss are distinct:
S1YS2TEMS3

There are 7! permutations of S1YS2TEMS3

But when we stop pretending we see that we have counted each


arrangement of SYSTEMS 3! times, once for each of 3!
rearrangements of S1S2S3.

7!
= 840
3 !
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 03: Graph Algorithms CSE506

Work SMART
not
HARD
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 03: Graph Algorithms CSE506

Graph Algorithms

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Graph Definitions
A graph G is denoted by G = (V, E) where
V is the set of vertices or nodes of the graph
E is the set of edges or arcs connecting the vertices in V
Each edge E is denoted as a pair (v,w) where v,w V
For example in the graph below

2 V = {1, 2, 3, 4, 5, 6}
E = {(1, 2) (2, 5) (3, 6) (4, 6) (5, 6)}
1 3 This is an example of an unordered
or undirected graph

6 4

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Graph Definitions (contd.)


If the pair of vertices is ordered then the graph is a directed
graph or a di-graph
2

1 3 Here,
V = {1, 2, 3, 4, 5, 6}
E = {(1, 2) (2, 5) (5, 6) (6, 3) (6, 4)}
6 4

5
Vertex v is adjacent to w iff (v,w) E
Sometimes an edge has another component called a weight or
cost. If the weight is absent it is assumed to be 1

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Graph Definitions: Path


A path is a sequence of vertices w1, w2, w3, ....wn such that
(wi, wi+1) E
Length of a path = # edges in the path
A loop is an edge from a vertex onto itself. It is denoted by
(v, v)
A simple path is a path where no vertices are repeated
along the path
A cycle is a path with at least one edge such that the first
and last vertices are the same, i.e. w1 = wn

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Applications of Graphs
Driving Map
Edge = Road
Vertex = Intersection
Edge weight = Time required to cover the road
Airline Traffic
Vertex = Cities serviced by the airline
Edge = Flight exists between two cities
Edge weight = Flight time or flight cost or both
Computer networks
Vertex = Server nodes
Edge = Data link
Edge weight = Connection speed
CAD/VLSI

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Spanning Tree

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Minimum Spanning Tree

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Motivation
For an electrical circuit certain pins have to be grounded. Finding the
arrangement of wires (connecting those pins) that uses the least amount of
wire.
Let a G(V,E) be a graph such that (u, v) E and a weight w(u,v)
corresponding to wire needed to join u and v.
We are looking for an acyclic subset T E that connects all vertices and
whose total weight w(T) is minimized.

w(T ) = (u ,v )T w(u , v)

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Minimum Spanning Tree


Problem: given a connected, undirected, weighted
graph, find a spanning tree using edges that minimize
the total weight

6 4
5 9

14 2
10
15

3 8

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Minimum Spanning Tree

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Minimum Spanning Tree Problem

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Generic Algoithm for MST problem

How to find it?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Finding a safe edge

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Implementing Priority Queue

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Implementing Priority Queue

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Analysis

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Example

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 03: Graph Algorithms CSE506

Prims Algorithm Analysis

O(|V|2) if matrix

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like