graphs: as a mathematics branch

34
06/14/22 ACM-ICPC 1 Graphs: As a mathematics branch Map of Königsberg Leonhard Paul Euler 1707-1783 cross every bridge exactly once and return to the same place is no other way but through the bridges to cross the rivers) solution is considered as the first Graph theorem in history 1 3 4 2

Upload: rylee-hubbard

Post on 01-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

Graphs: As a mathematics branch. Map of Königsberg. 1. 2. 3. 4. Leonhard Paul Euler 1707-1783. Can we cross every bridge exactly once and return to the same place? ( There is no other way but through the bridges to cross the rivers ). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 1

Graphs: As a mathematics branch

Map of Königsberg

Leonhard Paul Euler 1707-1783

Can we cross every bridge exactly once and return to the same place? (There is no other way but through the bridges to cross the rivers)

Euler’s solution is considered as the first Graph theorem in history.

1

3

4

2

Page 2: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 2

Eulerian Graph

Map of Königsberg

Leonhard Paul Euler 1707-1783

Starting from a vertex, can we travel every edges exactly once and return to the same vertex?

If we can, such a path is called an Eulerian Circuit

1

2

4

3

Theorem [Euler 1737]: A graph has an Eulerian Circuit iff the degree of every vertex is even.

No!

The first Graph theorem in history.

Page 3: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 3

Graphs: Algorithmic approach

G = (V, E)

Example,

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

V: a set of verticesE: a set of edges, E V V 1

2 5

3 4

6

1. Many problems are unsolvable (usually are associated to infinite graphs) ;2. Many problems are intractable (NPC problems),3. And yet, many applicative problems are manageable;

(in terms of |V| and |E|)

Page 4: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 4

Undirected Graphs

Example,

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

1

2 5

3 4

6

G= (V,E) is an undirected graph, if (v, w) E (w, v) E

V: = {1,2,3,4,5,6} E: = { {1,2}, {1,5}, {2,3}, {2,5}, {3,4}, {4,5}, {4,6} }

Page 5: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 5

Weighted Graphs

V: = {1,2,3,4,5,6} E: = {(1,2,3), (1,5,2.3), (2,3,4.5), (2,5,2/3), (3,4,7.1), (4,5,10), (4,6,12)}

Weighted graph: G= (V,E), where E V V Q

rational numbers

Example,

Every edge in E has a value associated to it.

1

2 5

3 4

6

3 2.3

4.5

2/3

7.1

1012

Page 6: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 6

A walk on a graph

(v1, v2,.... vn) Vn , such that (vi , vi+1) E, for 1 i < n, is called a walk of G= (V,E)

(1,2,4,7,9,8,5,4,7,6,5,2,3)0

2 4

3 5

7

81

6

9

Example:

a walk

Page 7: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 7

More Terminologies(v1, v2,.... vn) Vn , such that (vi , vi+1) E, for 1 i < n, is

called a walk of G= (V,E)

1. A path is a walk such that no vertex in the walk is repeated.

2. A trail is a walk such that no edge in the walk is repeated. (Any path is a trial)

3. A circuit is a closed trial, i.e, v1 = vn.

4. A cycle is a circuit such that v1 = vn is only repeated vertex.

5. A spanning is a walk such that every vertex is included6. A Eulerian circuit is a circuit that contains every edge. 7. A Hamiltonian cycle is a spanning cycle.

Page 8: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 8

A Walk: 1,2,4,7,9,8,5,4,7,6,5,2,3 (not a trial)

0

2 4

3 5

7

81

6

9

Page 9: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 9

A Trial: 1,2,4,7,9,8,5,4,0,7,6,5,2,3 (not a path)

2 4

3 5

7

81

6

9

0

A Circuit: 1,2,4,7,9,8,5,4,7,6,5,2,3,1 (not a cycle)

Page 10: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 10

A Path: 1,2,4,0,7,6,5,3

2 4

3 5

7

81

6

9

0

A Cycle: 1,2,4,0,7,6,5,3,1

There are many other cycles in this graph

Page 11: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 11

A graph without any cycle in it is called acyclic

2 4

3 5

7

1

6

9

0

8

An acyclic Graph

Page 12: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 12

Representation of Graphs. (Data Structures)

2 4

3 5

7

1

6

9

0

8

Adjacency Matrix

0 1 2 3 4 5 6 7 8 9

0 1 1 1 1

1 1

2 1 1 1 1 1

3 1 1

4 1 1

5 1 1 1 1

6 1 1

7 1 1 1

8 1 1 1

9 1

Good for dense graph.

Page 13: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 13

Representation of Graphs.

Adjacency Matrix for a Weighted Graph

0 1 2 3 4 5

0 0 2.3

1 0 4.5 2/3

2 0 7.1

3 0 10 12

4 0

5 0

0

1 4

2 3

5

3 2.3

2/3

7.1

1012

4.5

Page 14: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 14

Representation of Graphs. (Data Structures)

2 4

3 5

7

1

6

9

0

8

Adjacency List

0 2 4 7

1

2 1 3 4 5

3 1

4 7

5 3 4 6

6 1

7 6 9

8 5 9

9

A commonly used representation

Page 15: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 15

Representation of Graphs.

Adjacency List for a Weighted Graph

0 (1, 3) (4, 2.3)

1 (2, 4.5) (4, 2/3)

2 (3, 7.1)

3 (4, 10) (5, 12)

4

5

0

1 4

2 3

5

3 2.3

2/3

7.1

1012

4.5

How to implement?

Page 16: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 16

Representation of Graphs. (Data Structures)

Adjacency List

0

1

2

3

4

5

6

7

8

9

2 4 7

1 3 4

3 4 6

5

1

7

1

6 9

5 9

1. Should be easy to insert/delete vertices and edges

2. Should be easy to retrieve.

3. Should not use to much memory.

array (dynamic) linked lists

vectordequestacksetmap

Page 17: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 17

<set>

#include <iostream>#include <set>..............set<int> S;..............while (....) {

int e; // an element of SS.insert(e);S.insert(e);

}set<int>::iterator itr;itr = S.begin();S.erase(itr);

for (itr= S.begin(); itr != S.end(); itr++)cout << *itr;

10

12

8 15

6

3

95

Red-Black Tree

Page 18: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 18

<map>

#include <iostream>#include <map>..............map<int, double> M;..............int i;double w;while (....) { // e is an element of M

pair e = make_pair(i,w); M.insert(e);M.insert(e);

}map<int, double>::iterator itr;itr = M.begin();M.erase(itr);

for (itr= M.begin(); itr != M.end(); itr++) cout << “\n(“ << itr->first << “,” << itr->second << “)”;

10, 1.2

12, 1.2

8, 5.6 15, 0.3

6, 3.4

3, 3.4

9, 0.15, 2.8

Red-Black Tree

key, associated value in, double

Page 19: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 19

Using STL to implement Graphs

typedef map<int, double> AdjacencyList; typedef map<int, AdjacencyList> Graph;

0

1 4

2 3

5

3 2.3

0.6

7.1

10 124.5

0 (1, 3) (4, 2.3)

1 (2, 4.5) (4, 0.6)

2 (3, 7.1)

3 (1, 6.3) (4, 10) (5, 12)

4

5

3,

4,

2, 5,

1,

0,

4, 10

5, 12 1, 6.3

6.3

4, 2.3

1, 3.02, 4.5

4, 0.6

3, 7.1

Graph G;

G[3]

(G[3])[5] = 12

(G[1])[4] = 0.6

G[1]G[2]

G[0]

G[4]

G[5]

Page 20: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 20

Remove a vertex

0

1

2

3

4

5

6

7

8

9

2 4 7

1 3 4

3 4 6

5

1

7

1

6 9

5 9

remove 7

Some basic graph operations

Page 21: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 21

Given a graph, determine is the graph is acyclic

2 4

3 5

7

1

6

9

0

8

Not An acyclic Graph

No vertex in this cycle has in-degree 0

Page 22: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 22

Procedure to determine if a given graph is acyclic

2 4

3 5

7

1

6

9

0

8

Repeatedly remove vertices with in-degree 0;

If we can’t remove all vertices, then the graph is not acyclic

Page 23: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 23

Procedure to determine if a given graph is acyclic

2 4

3 5

7

1

6

9

0

8

Repeatedly remove vertices with in-degree 0;

If all vertices can be removed, then the given graph is acyclic

Page 24: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 24

Topological Sort (topological order)

Let G = (V, E) be a directed graph with |V| = n.

1. { v1, v2,.... vn } = V, and2. for every i and j with 1 i < j n, (vj , vi) E.

17 6 50 3 42

A Topological Sort is a sequence v1, v2,.... vn, such that

1

7

6

5

0

3

42

There are more than one such order in this graph!!

Page 25: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 25

If the graph is not acyclic, then there is no topological sort for the graph.

2 4

3 5

7

1

6

9

0

8

Not an acyclic Graph

No way to arrange vertices in this cycle without pointing backwards

Page 26: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 26

Algorithm for finding Topological Sort

Principle: vertex with in-degree 0 should be listed first.

Algorithm:

Repeat the following steps until no more vertex left:

1. Find a vertex v with in-degree 0;

Input G=(V,E)

2. Remove v from V and update E3. Process v

If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic

2 1 7 2 7

1

Page 27: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 27

Topological Sort (topological order) of an acyclic graph

17 6 50 3 42

1

7

6

0

3

4

52

Page 28: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 28

2 4

3 5

7

1

6

9

0

8

the graph is not acyclic

Fail to find a topological sort (topological order)

1 2 8

Page 29: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 29

2 4

3 5

7

1

6

9

0

8

This graph is acyclic

Topological Sort (topological order) of an acyclic graph

0 2 8 5 4 3 7 9 6 1

Page 30: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 30

1. Back Tracking....2. Greedy Algorithms: Algorithms that make decisions

based on the current information; and once a decision is made, the decision will not be revised

3. Divide and Conquer....4. Dynamic Programming.....

Common Algorithm Techniques

Problem: Minimized the number of coins for change.

In real life, we can use a greedy algorithm to obtain the minimum number of coins. But in general, we need dynamic programming to do the job

Page 31: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 31

Greedy Algorithms Dynamic Programming

Problem: Minimized the number of coins for 66¢

{ 25¢, 10¢, 5¢, 1¢ } { 25¢, 12¢, 5¢, 1¢ }

25¢ 2 50¢

10¢ 1 10¢

5¢ 1 5¢

1¢ 1 1¢

5 66¢

25¢ 2 50¢

12¢ 1 12¢

5¢ 0 0¢

1¢ 4 4¢

7 66¢

25¢ 2 50¢

12¢ 0 0¢

5¢ 3 15¢

1¢ 1 1¢

6 66¢

Greedy method Greedy method Dynamic method

16¢

6¢ 1¢ 0¢

16¢

4¢ 4¢ 0¢

Page 32: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 32

Unweighted Graphs Weighted Graphs

Starting Vertex

wFinal Vertex

Starting Vertex

w Final Vertex

1

52

1 5

9

7

1

Both are using thegreedy algorithm.

0

1

22

2

1

0

2 52 3

3 73

3

12

7

1088

Finding the shortest path between two vertices

Page 33: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 33

Starting Vertex

Final Vertex

0,-1

1,a

2,a2,a

2,a

1,a

Construct a shortest path Map

c

d e

Starting Vertex

f

nFinal Vertex

a

b

x w-1,y

w,x

Page 34: Graphs: As a mathematics branch

04/20/23 ACM-ICPC 34

Construct a shortest path Map for weighted graph

1 2

3 4

Starting Vertex

5

n Final Vertex

1

52

1 5

9

7

1

0

Starting Vertex

Final Vertex

0,-1

5,0

7,13,1

10,2

2,0

w-1,y

w,x

: decided

3,1

8,4