93
Unit 6. Graphs Algorithms and Data Structures (ADS) 1 Author: Isabel Segura-Bedmar Grado en Ciencia e Ingeniería de Datos, 2018-2019

Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Unit 6. Graphs

Algorithms and Data Structures (ADS)

1Author: Isabel Segura-Bedmar

Grado en Ciencia e Ingeniería de Datos, 2018-2019

Page 2: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

2

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal

Page 3: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Introduction to Graphs

3

Linear data structures:

Page 4: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Introduction to Graphs

4

Non-linear data structures:

Page 5: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Introduction to Graphs

5

Non-linear data structures:

Nodes or verticesEdges

No rules for connections

Page 6: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

6

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal

Page 7: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

7

Graph:

A graph G is an ordered pair of a set V of vertices and a set E of edges

G=(V,E)

Page 8: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

8

V = { v1, v2, v3, v4, v5, v6, v7, v8}

How can we represent an edge?

Page 9: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

9

(u,v)!=(v,u) if u!=v

{u,v} ={v,u}

Types of edges:

Page 10: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

10

directed vs. undirected

a directed graph(digraph) an undirected graph

Page 11: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

11

V = { v1, v2, v3, v4, v5, v6, v7, v8}E= { {v1, v2}, {v1, v3}, {v1, v4},{v2,v5}, {v2,v6},

{v3,v7}, {v4,v8}, {v5,v8},{v6,v8}, {v7,v8}}

ΙVΙ =number of verticesΙEΙ =number of edges

ΙVΙ = 8, ΙEΙ=10

Page 12: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

12

Social Network (undirected graph)

Isabel

CésarLeti

Moni

Chus

Juan

David

Ana

Diego

Tom Pablo

Fede

Fran

Page 13: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

13

How to suggest some new friends to Isabel?

Isabel

CésarLeti

Moni

Chus

Juan

David

Ana

Diego

Tom Pablo

Fede

Fran

Page 14: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

14

Isabel

CésarLeti

Moni

Chus

Juan

David

Ana

Diego

Tom Pablo

Fede

Fran

Page 15: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

15

Isabel

CésarLeti

Moni

Chus

Juan

David

Ana

Diego

Tom Pablo

Fede

Fran

Find all nodes having length of shortest path from Isabel equal to 2

Page 16: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

16

PageC

PageA PageB

PageG

PageH

PageD

PageE

PageF

PageE has a link to PageF

World Wide Web (it’s a directed graph)

Pages as vertices (have a unique URL)

Page 17: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

17 weighted graph

Leganés Getafe

Pta Toledo Colmenarejo

Aranjuez

1045

48

82

4614

13

35

41

39

UC3M Campuses (distance in kilometers)

17

Page 18: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

18

Type of edges

loop

When are they necessary?

Page 19: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

19

World Wide Web

PageC

PageA PageB

PageG

PageH

PageD

PageE

PageF

A web page may contain a link to itself

Page 20: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

20

Type of edges Multi-edge (parallel edges)

Sevilla

Madrid

AVE9

12AVE3

01AV

E508

Page 21: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

21

● Loops and parallel edges lead to complicate graph algorithms

● A graph is simple if it has no loops or parallel edges.

Page 22: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

22

What is the maximum possible number of edges in a simple directed graph?

A B

C D

|V| = 4|E| = 0 (minimum)

|V| = 4|E| = 12 (maximum)

If /V/ = n, each vertex may have n-1 edges. Therefore, 0<= /E/<=n(n-1), if directed

Page 23: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

23

What is the maximum possible number of edges in a simple undirected graph?

If /V/ = n, each vertex may have n-1 edges. Therefore, 0<= /E/<=n(n-1)/2, if directed

A B

C D

|V| = 4|E| = 0 (minimum)

|V| = 4|E| = 6 (maximum)

Page 24: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

24

● A graph is dense if the number of its edges is close to its maximum possible number (≈ |V|2)

A B

C D

Page 25: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

25

● A graph is sparse if the number of its edges is close to its number of vertices (≈|V|)

A B

C D

Page 26: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

26

● Knowing if a graph is dense or sparse can help us to select the most appropriate data structure to represent it.

A B

C D

A B

C Ddense sparse

Page 27: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

27

● Path is a sequence of vertices where each adjacent pair is connected by an edge

A B

C D

E G

F

H

<A,B,F,E,G>

It is a a simple path (vertices are not repeated)

Page 28: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

28

A B

C D

E G

F

H

<A,B,F,E,G,F,E> This is not a simple path (two repeated vertices and one edge)

Page 29: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

29

● A graph is strongly connected if there is a path from any vertex to any other vertex.

A B

C D

A B

Cstrongly connected

A B

CWeakly connected

Page 30: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

30

● Simple cycle is a close walk with no repetition other than start and end.

A

C D

B

Page 31: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph properties

31

Acyclic graph is a graph with no cycles.

A B

C D

EA B

C D

E

Undirected acyclic graph directed acyclic graph (DAG)

Page 32: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

32

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal

Page 33: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph representation

33

G=(V,E), V vertices, E edges

A B

C D

E

F

How can we create and store a graph in computer memory?

Page 34: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

34

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal

Page 35: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph representation: Adjacency Matrix

35

ABCDEF

Vertex list

A B

C D

E

F

0

1

2

3

4

5We can use a Python list to store the vertices. Each vertex is represented by an index.

Page 36: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

36

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 1 1 0 0 0

1 0 0 1 1 0

1 0 0 0 0 1

0 1 0 0 1 0

0 1 0 1 0 0

0 0 1 0 0 0

0

1

2

3

4

5

0 1 2 3 4 5

1, if {i, j} is an edgeMij =

0, otherwise

0 1

2 3

4

5

Page 37: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

37

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 1 1 0 0 0

1 0 0 1 1 0

1 0 0 0 0 1

0 1 0 0 1 0

0 1 0 1 0 0

0 0 1 0 0 0

0

1

2

3

4

5

0 1 2 3 4 50 1

2 3

4

5

undirected graph Mij = Mji

Page 38: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

38

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

5

1

7

8

9

60

1

2

3

4

5

∞ 5 1 ∞ ∞ ∞5 ∞ ∞ 6 9 ∞1 ∞ ∞ ∞ ∞ 7∞ 6 ∞ ∞ 8 ∞∞ 9 ∞ 8 ∞ ∞∞ ∞ 7 ∞ ∞ ∞

0

1

2

3

4

5

0 1 2 3 4 5

Representation of weighted graph

Page 39: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

39

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 0 0 00 0 00 0 0 0

0 0 0 00 0 0 00 0 0 0 0

0

1

2

3

4

5

1 11 1 1

111 11

11

0 1

3

4

5

Operations: Time complexity

Finding adjacent nodes

|V| = n

0 1 2 3 4 5

O(n)

Page 40: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

40

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 0 0 00 0 00 0 0 0

0 0 0 00 0 0 00 0 0 0 0

0

1

2

3

4

5

1 11 1 1

111 11

11

0 1

2 3

4

5

Operations: Time complexityChecking if two givennodes are adjacent (M(1,3)?)

O(1)

|V| = n

0 1 2 3 4 5

Page 41: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

41

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 0 0 00 0 00 0 0 0

0 0 0 00 0 0 00 0 0 0 0

0

1

2

3

4

5

1 11 1 1

111 11

11

0 1

2 3

4

5

Space complexity:If |V| = n, O(n2)

Page 42: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

42

Graph representation: Adjacency Matrix

● In terms of time complexity, adjacency matrix is an efficient data structure.

● However, in terms of space complexity, it is too costly. ● Adjacency matrix is a good representation when n2 is

small or the graph is dense. ● However, most real graphs are sparse (for example,

WWW).

Page 43: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

43

Graph representation: Adjacency Matrix

Isabel

CésarLeti

Moni

Chus

Juan

David

Ana

Diego

Tom

PabloFede

If |V| = 109 space= 1018

Suppose avg. number of friends ≈ 1000 |E| = (109*103)/2 = 1012/2<< 1018

Page 44: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

44

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal

Page 45: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

45

Graph representation: Adjacency Matrix

ABCDEF

A B

C D

E

F

0

1

2

3

4

5

0 0 0 00 0 00 0 0 0

0 0 0 00 0 0 00 0 0 0 0

0

1

2

3

4

5

1 11 1 1

111 11

11

0 1

2 3

4

5|V|=6, matrix=6x6

1 0 0 1 1 00 1 2 3 4 5

List of size 6Adjacent vertices for B?

Page 46: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

46

Graph representation: Adjacency List

A B

C D

E

F

0 1

2 3

4

5

Connections for B:

1 0 0 1 1 00 1 2 3 4 5

Suppose Facebook has 109 users dimension of row is 109

If B has 1000 friends:Numbers of 1: 1000 ≈ 1 KBNumbers of 0: 109- 1000 ≈ 1 GB

Page 47: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

47

Graph representation: Adjacency List

A B

C D

E

F

0 1

2 3

4

5

Connections for B:

1 0 0 1 1 00 1 2 3 4 5

0 3 4

Python List

0 3 4 None1

Linked List

You can use:a) A Python List, orb) A Linked List

Page 48: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

48

Graph representation: Adjacency List

0 3 4

A B

C D

E

F

1

2 3

4

5

11 20

0 52

1 43

1 3425

Adjacency list can be represented as a list of lists

0

ABCDEF

0

1

2

3

4

5

Page 49: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

49

Graph representation: Adjacency ListA B

C D

E

F

1

2 3

4

5

0

0 3 4 None1

1 2 None0

0 5 None2

1 4 None3

1 3 None4

2 None5

Adjacency list= List of Linked Lists

Page 50: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

50

Graph representation: Adjacency List

1

(1,5) (2,1)0

2

A B

C D

E

F

5

1

11

8

9

6 (0,5) (3,6) (4,9)

Adjacency list(weighted graph)

1

2 3

4

5

0

(0,1) (5,11)

3 (1,6) (4,8)

4 (1,9) (3,8)

5 (2,11)

Each adjacent vertex is represented with a pair (i,j) where i is the index of the vertex and j the related weight.

Page 51: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

51

Graph representation: Matrix versus List

A B

C DE

F

0 1 1 0 0 01 0 0 1 1 01 0 0 0 0 10 1 0 0 1 00 1 0 1 0 00 0 1 0 0 0

0

1

2

3

4

5

0 1

2 3

4

5

0 1 2 3 4 5

1 2

0 50 3 4

1 41 31

0

1

2

3

4

5

Operations:✔ adjacent nodes for i?✔ (i,j) is an edge?

O(n) O(1)O(1) O(n)

Page 52: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

52

Graph representation: Matrix versus List

A B

C DE

F

0 1 1 0 0 01 0 0 1 1 01 0 0 0 0 10 1 0 0 1 00 1 0 1 0 00 0 1 0 0 0

0

1

2

3

4

5

01

2 3

4

5

0 1 2 3 4 5

1 2

0 50 3 4

1 41 31

0

1

2

3

4

5

Space = O(n2) Space = O(e)

Most real graphs are sparse (|E|≈|V|<<|V|2)

Page 53: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

53

Graph representation

● Most real graphs are sparse (|E|≈ |V|<<|V|2)● Adjacency matrix, space complexity O(|V|2), time

complexity O(|V|) (sometimes O(1)). It is a good solution when the graph is dense or n2 is small.

● Adjacency list, space complexity: O(|E|) <<O(|V|2) (if graph is sparse). Time complexity: O(|V|)

Page 54: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

54

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal○ Breadth-first Traversal○ Depth-first Traversal

Page 55: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

55

Graph traversal

Visiting all the nodes of the graph

A B

G D

C

F E

Traveling Salesman Problem (TSP)

Page 56: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

56

Graph traversalVisiting all the nodes of the graph

1) Breadth-first traversal (BFS)2) Depth-first traversal (DFS)

A

CB

E F

G

D

H

Page 57: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

57

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Idea: visit nodes in layers (levels). It’s similar to Level-order traversal in trees

Page 58: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

58

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Output: A

Page 59: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

59

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Output: A B C D

Page 60: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

60

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Output: A B C D E F

Page 61: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

61

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Output: A B C D E F G

Page 62: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

62

Graph traversal: Breadth-first traversal (BFS)A

CB

E F

G

D

H

Output: A B C D E F G H

Page 63: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

63

Graph traversal: Breadth-first traversal (BFS)

q

0

1 2 3

4 5

6

7

● Queue ● A list to store the visited

nodes

A

CB

E F

G

D

H

visited

Page 64: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

64

Graph traversal: Breadth-first traversal (BFS)

Start by a node (for example, vertex=0) , and put it into the queue

0

1 2 3

4 5

6

7

A

CB

E F

G

D

H

0

1 2 3

4 5

7

A

0q

Page 65: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

65

Graph traversal: Breadth-first traversal (BFS)

While the queue is not empty, repeat:1. Remove the head from the queue2. Print it and save it into the visited

list

output:

A

CB

E F

G

D

H

0

1 2 3

4 5

6

7

V=0

A

0

0

q

0visited

Page 66: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

q

B C D

While the queue is not empty, repeat:1. Remove the head from the queue2. Print it and save it into the visited

list3. Get its adjacent nodes and put

them into the queue (only not visited)

A

CB

E F

G

D

H

0

1 2 3

4 5

6

7

1 2 3

B C D

Page 67: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)While the queue is not empty, repeat:1. Remove the head from the queue2. Print it and save it into the visited list3. Get its adjacent nodes and put them into

the queue (only not visited)

A

CB

E F

G

D

H

0

1 2 3

4 5

6

7

q

BB

E FB (1) has as adjacent nodes: E(4), F (5)

1 2 3 4 5

output:

V=1

0 1

0 1visited

Page 68: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E F

G

D

H

0

1 2 3

4 5

6

7

q

C(2) has no adjacent nodes

2 3 4 5

C

output:

V=2

0 1 2

0 1 2visited

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)

Page 69: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

D(3) has one only adjacent node, F(5), which is already in the queue

3 4 5

D

F

output:

V=3

0 1 2 3

0 1 2 3visited

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)

Page 70: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

E(4) has one only adjacent node, G(6)

4 5 6E

G

F

output:

V=4

0 1 2 3 4

0 1 2 3 4visited

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)

Page 71: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

F(5) does not have any adjacent node

5 6F

output:

V=5

0 1 2 3 4 5

0 1 2 3 4 5visited:

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)

Page 72: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

G(6) has one only adjacent node, H(7)

6 7F

G

Houtput:

V=6

0 1 2 3 4 5 6

0 1 2 3 4 5 6

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)

visited:

Page 73: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

H(7) does not have any adjacent node

7F

H

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)∑

output:

V=7

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7visited

Page 74: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)

A

CB

E

G

D

H

0

1 2 3

4 5

6

7

q

The queue is empty and all the nodes have already visited!!!

F

output: 0 1 2 3 4 5 6 7

While the queue is not empty, repeat:1. Remove the head from the queue.2. Print it and save it into the visited list3. Get its adjacent nodes and put them

into the queue (only not visited)∑

Page 75: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Breadth-first traversal (BFS)Algorithm bst(vertex):

q=Queueu() #queue for adjacent verticesvisited=[]q.enqueue(vertex)while q.isEmpty()==False:

current=q.dequeue()print(current)visited.append(current)adjLst=getAdjacents(current)for v in adjLst:

if v not in visited:q.enqueue(v)

Page 76: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Index

76

● Introduction to Graphs● Graph properties● Graph representation:

○ Adjacency Matrix.○ Adjacency List.

● Graph Traversal○ Breadth-first Traversal○ Depth-first Traversal

Page 77: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H

Select a node and go forward as far as possible along a branch, if not then, backtrack

Page 78: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)

Select a node and go forward as far as possible along a branch, if not then, backtrack

A

CB

E F

G

D

H Output: A,

Page 79: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)

Select a node and go forward as far as possible along a branch, if not then, backtrack

Output: A,B

A

CB

E F

G

D

H

Page 80: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H

Select a node and go forward as far as possible along a branch, if not then, backtrack

Output: A, B, E

Page 81: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G

Page 82: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H

Page 83: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H

Page 84: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H

Page 85: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H

Page 86: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F

Page 87: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F

Page 88: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F

Page 89: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F

Page 90: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F, C

Page 91: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F, C, D

Page 92: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)A

CB

E F

G

D

H Output: A, B, E, G, H, F, C, D

D has an adjacent node F, which is visited. We have finished because all nodes are already visited!!!

Page 93: Unit 6. Graphs Algorithms and Data Structures (ADS)ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · Algorithms and Data Structures (ADS) Author: Isabel

Graph traversal: Depth-first traversal (BFS)

Algorithm depth(vertex, visited):print(vertex)visited.append(vertex)for v in getAdjacents(vertex):

if v not in visited[v]:depth(v,visited)

Note: visited is a list to store the nodes that we visit.