# v[ b ].push_back( a

(1)

## GRAPH

(2)

### COURSE OUTLINE

Basic Graph (review) Shortest Path

Minimum Spanning Tree

(3)

(4)

(5)

### MORE DEFINITIONS

weighted vs unweighted directed vs undirected

(6)

(7)

### UNDIRECTED GRAPH

vector< int > v[ N ] ; // an edge between a and b v[ a ].push_back( b ) ; v[ b ].push_back( a ) ;

### DIRECTED GRAPH

vector< int > v[ N ] ; // an edge from a to b v[ a ].push_back( b ) ;

(8)

### UNWEIGHTED GRAPH

vector< int > v[ N ] ; // an edge between a and b v[ a ].push_back( b ) ; v[ b ].push_back( a ) ;

### WEIGHTED GRAPH

typedef pair< int , int > ii ; vector< ii > v[ N ] ;

// an edge between a and b with weight w v[ a ].push_back( ii( b , w ) ) ;

v[ b ].push_back( ii( a , w ) ) ; // v[ a ].emplace_back( b , w ) ; // v[ b ].emplace_back( a , w ) ;

(9)

### TRAVERSING NEIGHBOR

vector< int > v[ N ] ;

// go through all vertices adjacent to a for ( int x : v[ a ] ) {

// x: a vertice directly linked to by "a"

}

typedef pair< int , int > ii ; vector< ii > v[ N ] ;

// go through all vertices adjacent to a for ( ii x : v[ a ] ) {

int b = x.first , w = x.second

// b: a vertice directly linked to by "a"

// w: weight(a, b) }

(10)

(11)

### PATH

A path on a graph is a sequence of vertices

satisfying ( )

1

2

3

n−1

n

i

i+1

(12)

### WEIGHT OF A PATH

The weight of a path is the sum

1

2

3

n−1

n

n−1i=1

i

i+1

(13)

### SHORTEST PATH

The shortest path from to is the path with minimum weight among all paths

satisfying and

1

2

3

n−1

n

1

n

(14)

(15)

(16)

### 0-1 BFS

Implement with a deque Time:

(17)

### SHORTEST PATH ON WEIGHTEDGRAPH

Floyd-Warshall Algorithm (All-pairs) Dijkstra's Algorithm (Single-source)

Bellman-Ford Algorithm (Single-source)

(18)

vs

(19)

### FLOYD-WARSHALL

No negative cycle

Dynamic Programming

// dis[ i ][ j ]: initialized to weight of edge(i, j) for ( int k = 1 ; k <= n ; k ++ )

for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ )

dis[ i ][ j ] = min( dis[ i ][ j ] ,

dis[ i ][ k ] + dis[ k ][ j ] ) ; // dis[ i ][ j ]: shortest path from i to j

(20)

easy to implement

(21)

Cannot handle negative cycle

3

(22)

### DIJKSTRA'S ALGORITHM

Cannot handle negative edge Single-source

Greedy

2

(23)

### ALGORITHM (INITIALIZE)

Let the source be

Maintain an unvisited vertice set (intially, ) Maintain an array , where stores the current

known shortest path from the source to vertice . (initially, )

Maintain a variable storing the vertice currently being handled.

Set and let .

(24)

### ALGORITHM (MAIN PART)

Repeat the following procedure until is empty

For all vertices adjacent to , let it be , update with Remove from .

For all vertices in , find the vertice with smallest , and assign it to .

(25)

### ALGORITHM (FINALE)

now stores the shortest path from to

Time complexity: (Better for dense graph)

Can also be implemented in time with priority queue. (Better for sparse graph)

2

(26)

### SUMMARY

Why is it correct?

Why not negative edges?

(27)

### IMPLEMENTATION (INITIALIZE)

bool vis[ N ] ; int vcnt = 0 ; int dis[ N ] ;

memset( vis , false , sizeof vis ) ; dis[ s ] = 0 ;

int p = s ;

(28)

### IMPLEMENTATION (MAIN)

while ( true ) {

for ( ii x : v[ p ] ) if ( !vis[ x ] ) { int q = x.first , w = x.second ;

dis[ q ] = min( dis[ q ] , dis[ p ] + w ) ; }

vis[ p ] = true ; int nxtp = -1 ;

for ( int i = 0 ; i < n ; i ++ ) if ( !vis[ i ] ) {

if ( nxtp == -1 || dis[ i ] < dis[ nxtp ] ) nxtp = i ; }

if ( nxtp == -1 ) break ; else p = nxtp ;

}

(29)

### BELLMAN-FORD ALGORITHM

Can handle negative cycles Single-source

(30)

(31)

(32)

(33)

(34)

(35)

(36)

### V − 1

(37)

struct edge {

int u , v , w ; } ;

vector< edge > E ; int dis[ N ] ;

for ( int i = 1 ; i < n ; i ++ ) for ( edge ed : E )

dis[ ed.v ] = min( dis[ ed.v ] , dis[ ed.u ] + ed.w ) ;

Updating...

## References

Related subjects :