graph algorithm - skku

46
Graph Algorithm SWE2016-44

Upload: others

Post on 15-Mar-2022

11 views

Category:

Documents


0 download

TRANSCRIPT

Graph Algorithm

SWE2016-44

Introduction

2

Graph: Represents pair-wise relationship between a set of objects

1. Vertex (nodes)

2. Edges (arc)

Edge

Vertex

Introduction

3

Directed Graph (di-graph): have pair of ordered vertices (u,v)

Un-Directed Graph: have pair of unordered vertices, (u,v) and (v,u) are same

Graph Applications

4

1. Social Networks – Facebook, LinkedIn, etc

2. City-Road Network

City

B

City

C

City

A

3. Precedence Constraints

Task

B

Task

C

Task

ASuccess Failure

Graph Representation

5

There are generally two ways to represent a graph data structure

• Adjacency Matrix

• Adjacency List

Graph Representation

6

Adjacency Matrix: Represents graph with ‘V’ nodes into an VxV 0-1 matrix where Aij=1 means that vertex ‘i’ and ‘j’ are connected.

Example

Graph Adjacency Matrix

Graph Representation

7

Adjacency Matrix

Pros:

• Easy to implement.

• Removing an edge takes O(1) time.

• Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1) Search

Graph Representation

8

Adjacency Matrix

Cons:

• Consumes more spaces O(V2).

• Adding a vertex is O(V2) if there is to add a new vertex, one has to increase the storage for a |V|² matrix to (|V|+1)²

Graph Representation

9

Adjacency List: An array of linked lists is used. Size of the array is equal to number of vertices and each entry of array corresponds to a linked list of vertices adjacent to the index

Example

Adjacency ListGraph

Graph Representation

10

Adjacency List

Pros:

• Saves space O(|V|+|E|), worst case O(V2).

• Adding a vertex is easier.

Graph Representation

11

Adjacency List

Cons:

• Queries like whether there is an edge from vertex u to vertex v are not efficient and can be done O(V) Search

Graph Implementation (C)

12

Graph Implementation (C++)

13

Breadth First Search (BFS)

14

Breadth First Search

15

Idea: Traverse nodes in layersL1

L2

L3

L4

Problem: Since we have cycles, each node will be visited infinite times.

Solution: Use a Boolean visited array.

Implementation

16

17

1

2 3

4 5

6

1 2 3 4 5 6

0 0 0 0 0 0Visited :

Queue :

Print :

18

1

2 3

4 5

6

1 2 3 4 5 6

1 0 0 0 0 0Visited :

Queue : 1

Print : 1

19

1

2 3

4 5

6

1 2 3 4 5 6

1 0 0 0 0 0Visited :

Queue :

Print : 1

20

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 0 0 0Visited :

Queue :

Print : 1

2 3

21

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 0 0 0Visited :

Queue :

Print : 1

2 3

2

22

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 0 0 0Visited :

Queue :

Print : 1

3

2

23

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 1 1 0Visited :

Queue : 3

Print :

4

1 2

5

24

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 1 1 0Visited :

Queue : 4

Print :

5

1 2 3

25

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 1 1 1Visited :

Queue : 5

Print : 1 2 3 4

6

26

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 1 1 1Visited :

Queue : 6

Print : 1 2 3 4 5

27

1

2 3

4 5

6

1 2 3 4 5 6

1 1 1 1 1 1Visited :

Queue :

Print : 1 2 3 4 5 6

Complexity

28

Time Complexity:

V: Vertices

E: Edges

O(V+E)

Depth First Search (DFS)

29

Depth First Search

30

Idea: To go forward (in depth) while there is any such possibility, if not then, backtrack

Problem: Since we have cycles, each node may be visited infinite times.

Solution: Use a Boolean visited array.

Depth First Search

31

1

2 3

4 5

6

Stack

Output:

Depth First Search

32

1

1

1

2 3

4 5

6

Stack

Output:

Depth First Search

33

1

1

1

2 3

4 5

6

Stack

Output:

2

2

Depth First Search

34

1

1

1

2 3

4 5

6

Stack

Output:

2

24

4

Depth First Search

35

1

1

1

2 3

4 5

6

Stack

Output:

2

24

45 5

Depth First Search

36

1

1

1

2 3

4 5

6

Stack

Output:

2

24

45 5

6

6

Depth First Search

37

1

1

1

2

4 5

6

Stack

Output:

2

24

45 5 6

3

Depth First Search

38

1

1

1

2 3

4 5

6

Stack

Output:

2

24

45 5 6

3

3

Depth First Search

39

1

1

1

2 3

4 5

6

Stack

Output:

2

24

45 5 6 3

Depth First Search

40

1

1

1

2 3

4 5

6

Stack

Output:

2

24

4 5 6 3

Depth First Search

41

1

1

1

2 3

4 5

6

Stack

Output:

2

2 4 5 6 3

Depth First Search

42

1

1

1

2 3

4 5

6

Stack

Output: 2 4 5 6 3

Depth First Search

43

1

1

2 3

4 5

6

Stack

Output: 2 4 5 6 3

Implementation

44

Complexity

45

Time Complexity:

V: Vertices

E: Edges

O(V+E)

Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004

• https://www.geeksforgeeks.org