Graph Ooi Wei Tsang School of Computing, NUS 1 A graph consists - - PowerPoint PPT Presentation

graph
SMART_READER_LITE
LIVE PREVIEW

Graph Ooi Wei Tsang School of Computing, NUS 1 A graph consists - - PowerPoint PPT Presentation

Graph Ooi Wei Tsang School of Computing, NUS 1 A graph consists of edges and vertices. 2 v u A vertex u is a neighbor of v, if there is an edge from v to u. We say u is adjacent to v. The number of neighbors of a vertex is called degree.


slide-1
SLIDE 1

Graph

Ooi Wei Tsang School of Computing, NUS

1

slide-2
SLIDE 2

A graph consists of edges and vertices.

2

slide-3
SLIDE 3

A vertex u is a neighbor of v, if there is an edge from v to u. We say u is adjacent to v. The number

  • f neighbors of a vertex is called degree.

v u

3

slide-4
SLIDE 4

A weighted graph has a value associated with its edges.

4 3

  • 3
  • 1

2

4

slide-5
SLIDE 5

Direction of edges does not matter in a undirected graph.

5

slide-6
SLIDE 6

In a complete graph, every vertex is connected to every other vertices.

6

slide-7
SLIDE 7

A path consists of a sequence of vertices adjacent to each other.

7

slide-8
SLIDE 8

A cycle is a path that starts and ends with the same vertex.

8

slide-9
SLIDE 9

A graph is acyclic if it contains no cycle. It is cyclic otherwise.

9

slide-10
SLIDE 10

A undirected graph is connected if there is a path between any two vertices.

10

slide-11
SLIDE 11

A undirected graph is bipartite if we can partition the vertices into two sets and there are no edges between two vertices of the same set.

11

slide-12
SLIDE 12

A unconnected graph consists of two connected components.

12

slide-13
SLIDE 13

A connected, undirected, acyclic graph is called a tree.

13

slide-14
SLIDE 14

A weighted graph G = (V, E, w),

where

  • V is the set of vertices
  • E is the set of edges
  • w is the weight function

14

slide-15
SLIDE 15

V = { a, b, c } E = { (a,b), (c,b), (a,c) } w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) } a b c 4

  • 3

1

15

slide-16
SLIDE 16

adj(v) : set of vertices adjacent to vertex v adj(a) = {b, c} adj(b) = { } adj(c) = {b} a b c 4

  • 3

1

16

slide-17
SLIDE 17
  • How many edges are there in a undirected

complete graph with N vertices?

Review Questions

17

slide-18
SLIDE 18

Review Questions

  • u∈V

|adj(u)| = ?

18

slide-19
SLIDE 19

Example Applications

19

slide-20
SLIDE 20

Representing a social network. (u,v) in E if u knows v.

20

slide-21
SLIDE 21

Jeffrey Heer’s Social Network from Friendster (47471 people, 432430 edges)

21

slide-22
SLIDE 22

Social network of 9/11 terrorists

22

slide-23
SLIDE 23

Representing places and routes. (u,v) exists if there is a direct route from u to v. Weight w(u,v) is the distance or cost. We are

  • ften interested in finding the cheapest path between between

two places.

4 3 3 1 2 2

23

slide-24
SLIDE 24

24

slide-25
SLIDE 25

25

slide-26
SLIDE 26

26

slide-27
SLIDE 27

Possible moves in Rush Hour. Blue represents solutions. Green represents the shortest paths to solving the puzzle.

(from www.aisee.com)

27

slide-28
SLIDE 28

Implementation

28

slide-29
SLIDE 29

1 2 4

  • 3

1 Adjacency Matrix: Use a 2D array. Store w(u,v) in a[u] [v] if edge (u,v) exists. Store an invalid value otherwise.

4

  • 3

∞ ∞ ∞ ∞

1

1 2 1 2

29

slide-30
SLIDE 30

1 2 4

  • 3

1 Adjacency List: Use an array of link list. a[u] stores adj(u) and the associated weight.

1 2 1,4 2,-3 1,1

30

slide-31
SLIDE 31
  • How long does it take to delete an edge for

(a) adjacency matrix ? (b) adjacency list ?

31

slide-32
SLIDE 32
  • How long does it take to go through all

neighbors of a vertex v for (a) adjacency matrix ? (b) adjacency list ?

32

slide-33
SLIDE 33
  • How much space is needed to store a graph
  • f size N if we are using

(a) adjacency matrix ? (b) adjacency list ?

33

slide-34
SLIDE 34

1 2 4

  • 3

1 Adjacency List in Matrix: Use a 2D array. Each row is an array-representation of the adjacency list.

1,4 2,-3 1,1

1 2

34

slide-35
SLIDE 35

Avoid using pointers in competitive programming. Most of the time, graph are static (no insert/ delete after initialization). Maximum size is often given.

35

slide-36
SLIDE 36

typedef struct neighbor { int id; int weight; } neighbor; // N is max num of vertices; neighbor graph[N][N]; int num_of_vertices;

36

slide-37
SLIDE 37

1 2 4

  • 3

1 Edge List: Use a linked list of edges.

1,4 2,-3 1,1

37

slide-38
SLIDE 38

1 2 4

  • 3

1 Edge List: Use a array of edges.

1,4 2,-3 1,1 2 1

Edges Degree

38

slide-39
SLIDE 39

typedef struct edge { int from; int to; int weight; } edge; edge graph[MAX_NUM_OF_EDGES]; int num_of_edges;

39

slide-40
SLIDE 40

Pick the simplest implementation that meets the requirements.

40

slide-41
SLIDE 41

Graph Traversal

How to systematically visit the whole graph?

41

slide-42
SLIDE 42

Breadth-First Search

  • r BFS

42

slide-43
SLIDE 43
  • Basic idea: pick a source and visit the

vertices in increasing distance from the source

  • visit all vertices one hop away
  • visit all vertices two hops away etc.
  • Note: A vertex u is k-hop away from the v if

the shortest path from u to v consists of k edges.

43

slide-44
SLIDE 44

F D E A C B

Example: F is 3-hop away from A. E is 2-hop away from A.

44

slide-45
SLIDE 45

F D E A C B

Let A be the source. We first visit the source. I colored visited vertices yellow.

45

slide-46
SLIDE 46

F D E A C B

Next, visit the vertices that are one-hop away.

46

slide-47
SLIDE 47

F D E A C B

Next, visit the vertices that are two hops away. (i.e, all unvisited vertices that are neighbors of one-hop neighbor of A.

47

slide-48
SLIDE 48

F D E A C B

Edges that lead to undiscovered node during traversal are colored brown.

48

slide-49
SLIDE 49

3 2 2 1 2

These edges form the breadth-first tree. Level of vertices in the tree is the hop distance from source.

49

slide-50
SLIDE 50
  • An implementation needs to keep track of

vertices we have discovered.

  • To visit the vertices in increasing order of hop

distance, we need to visit the nodes the order we discover them (FIFO).

50

slide-51
SLIDE 51

F D E A C B

Q = new Queue enqueue source into Q while Q is not empty v = dequeue from Q mark v as visited for each neighbor u of v if u is not visited and not already in Q enqueue u into Q

51

slide-52
SLIDE 52

Review Questions

  • Suppose we want to keep track of breadth-

first tree by marking the edges in the tree as

  • brown. How should we change the

algorithm?

52

slide-53
SLIDE 53

F D E A C B

Q = new Queue enqueue source into Q while Q is not empty v = dequeue from Q mark v as visited for each neighbor u of v if u is not visited and not already in Q mark (v,u) as brown enqueue u into Q

53

slide-54
SLIDE 54

Review Questions

  • Suppose we want to keep track of hop

distance from the source. How should we change the algorithm?

54

slide-55
SLIDE 55

F D E A C B

Q = new Queue enqueue source into Q level[source] = 0 while Q is not empty v = dequeue from Q mark v as visited for each neighbor u of v if u is not visited and not already in Q level[u] = level[v] + 1 enqueue u into Q

55

slide-56
SLIDE 56

Review Questions

  • Can we always visit every vertex using the

previous algorithm?

56

slide-57
SLIDE 57

F D E A C B

57

slide-58
SLIDE 58

F D E A C B

If we pick F as the source, then we can’t visit A, B, and C, and need to visit them through another source.

58

slide-59
SLIDE 59

Mark all vertices as unvisited for each vertex v if v is not visited use v as source and run BFS

59

slide-60
SLIDE 60

Applications of BFS

60

slide-61
SLIDE 61

3 2 2 1 2

On an unweighted graph, the breadth-first tree tells us the shortest path from source to all the other vertices.

61

slide-62
SLIDE 62

2 2 2 1 1

The algorithm works for undirected graph too.

62

slide-63
SLIDE 63

? We can check if two vertices are connected using BFS.

63

slide-64
SLIDE 64

Depth-First Search

  • r, DFS

64

slide-65
SLIDE 65
  • Basic idea: Starting from a source,

repeatedly visit a neighbor of the current vertex until we hit a dead-end (no unvisited neighbors), then backtrack.

  • After we visit a vertex v, we visit all vertices

reachable from v.

65

slide-66
SLIDE 66

F D E A C B

Let A be the source.

66

slide-67
SLIDE 67

F D E A C B

Visit a neighbor of A (say, C).

67

slide-68
SLIDE 68

F D E A C B

Visit a neighbor of C (say, E).

68

slide-69
SLIDE 69

F D E A C B

Visit a neighbor of E (say, D).

69

slide-70
SLIDE 70

F D E A C B

D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.

70

slide-71
SLIDE 71

F D E A C B

Visit B.

71

slide-72
SLIDE 72

F D E A C B

Visit F.

72

slide-73
SLIDE 73

F D E A C B

F has no unvisited neighbor. Back to B. B has no unvisited neighbor. Back to C.

73

slide-74
SLIDE 74

F D E A C B

C has no unvisited neighbor. Back to A. A, the source, has no unvisited neighbor. Done!

74

slide-75
SLIDE 75
  • An implementation needs to keep track of

vertices we have discovered.

  • When backtrack, we need to go back to the

last vertex we visited. (LIFO).

75

slide-76
SLIDE 76

F D E A C B

S = new Stack push source onto S while S is not empty v = top of S if v has a unvisited neighbor u mark u as visited push u onto S else pop v from S

76

slide-77
SLIDE 77

Mark all vertices as unvisited for each vertex v if v is not visited use v as source and run DFS

77

slide-78
SLIDE 78

F D E A C B

  • What is the color of a vertex:

(a) before it is inserted into the stack ? (b) while it is inside the stack ? (c) after it is pop from the stack ?

D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.

78

slide-79
SLIDE 79

F D E A C B

A vertex can be in three states: unvisited, visiting, visited.

D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.

79

slide-80
SLIDE 80

S = new Stack push source onto S while S is not empty v = top of S if v has a unvisited neighbor u mark u as “visiting” push u onto S else pop v from S mark u as “visited”

F D E A C B

80

slide-81
SLIDE 81

proc DFS(u): // recursive version of DFS mark u as “visiting” for each unvisited neighbor v of u DFS(v) mark u as “visited”

F D E A C B

81

slide-82
SLIDE 82

Review Questions

  • True/False? : There is always a path from the

vertices in the stack to the vertex at the top

  • f the stack.
  • (Alternatively: There is always a path from a

vertex marked “visiting” to the current vertex.)

82

slide-83
SLIDE 83

Applications of DFS

83

slide-84
SLIDE 84

? We can check if two vertices are connected using DFS.

84

slide-85
SLIDE 85

? We can check if a graph is acyclic/cyclic using DFS.

85

slide-86
SLIDE 86

? There is a cycle iff we found an edge from current vertex to a visiting vertex (called backward edge)

86

slide-87
SLIDE 87

proc DFS(u): mark u as “visiting” for each neighbor v of u if v is marked as “visiting” we found a cycle! else if v is marked as “unvisited” DFS(v) mark u as “visited”

87

slide-88
SLIDE 88

Topological Sort

Goal: Given a directed acyclic graph, order the vertices such that if there is a path from u to v, then u appears before v in the output.

88

slide-89
SLIDE 89

F D E A C B

BACFED? BCAFED? BFACED? Goal: Given a directed acyclic graph, order the vertices such that if there is a path from u to v, then u appears before v in the output.

89

slide-90
SLIDE 90

F D E A C B

Idea: The first vertex marked “visited” can appear last in the topological order.

90

slide-91
SLIDE 91

F D E A C B

Now, we remove that vertex from consideration, and repeat -- the next vertex marked as visited can appear last in the topological sort order.

91

slide-92
SLIDE 92

proc DFS(u): for each unvisited neighbor v of u DFS(v) push u onto a stack

To output in topological sort order, pop from stack and print after completing DFS.

92

slide-93
SLIDE 93

Dijkstra’s Algorithm

93

slide-94
SLIDE 94
  • Problem: Given a weighted graph G and a

vertex v in G, find the shortest (or least cost) path from v to all other vertices.

  • Restrict ourselves to positive weight.

Single-Source Shortest Path

94

slide-95
SLIDE 95

F D E A C B

Shortest Path from A to D = A-C-E-D (Cost = 8) 5 5 1 3 1 2 3 1 4

95

slide-96
SLIDE 96
  • Must keep track of smallest distance so far.
  • If we found a new, shorter path, update the

distance.

96

slide-97
SLIDE 97

10 6

Let d[v] be the current known shortest distance from u to v. d[v] = 6, d[w] = 10 2 u v w

97

slide-98
SLIDE 98

8 6

We just found a shorter path from u to w. Update d[w] = d[v] + cost(v,w). We call this step relax(v,w). 2 u v w

98

slide-99
SLIDE 99

proc relax (v,w): Let d = d[v] + cost(v,w) if d[w] > d d[w] = d

99

slide-100
SLIDE 100

8 6

If d[w] is the smallest among the “remaining” vertices, then d[w] is the smallest possible (can’t be relaxed further) u

6 12 11

w

100

slide-101
SLIDE 101

∞ ∞ ∞ ∞ ∞

At the beginning, we know d[A]. But the rest is unknown and is set to infinity. 5 5 1 3 1 2 3 1 4

101

slide-102
SLIDE 102

∞ ∞ ∞ 5 ∞

Relax all neighbors of A. 5 5 1 3 1 2 3 1 4

102

slide-103
SLIDE 103

∞ ∞ ∞ 5 ∞

Pick a white vertex with smallest d[ ]. Color it yellow. 5 5 1 3 1 2 3 1 4

103

slide-104
SLIDE 104

∞ 10 6 5 8

Relax all neighbors of this vertex. 5 5 1 3 1 2 3 1 4

104

slide-105
SLIDE 105

∞ 10 6 5 8

Repeat: pick a white vertex with smallest d[ ]. 5 5 1 3 1 2 3 1 4

105

slide-106
SLIDE 106

∞ 8 6 5 8

Relax its neighbors 5 5 1 3 1 2 3 1 4

106

slide-107
SLIDE 107

∞ 8 6 5 8

5 5 1 3 1 2 3 1 4

107

slide-108
SLIDE 108

11 8 6 5 8

5 5 1 3 1 2 3 1 4

108

slide-109
SLIDE 109

11 8 6 5 8

5 5 1 3 1 2 3 1 4

109

slide-110
SLIDE 110

11 8 6 5 8

5 5 1 3 1 2 3 1 4 Everyone is yellow. Done!

110

slide-111
SLIDE 111

proc Dijkstra(s): for each vertex v in G d[w] = infinity color[w] = white d[s] = 0

111

slide-112
SLIDE 112

while there exists a white vertex let u be a white vertex with smallest d color[u] = yellow for each neighbor v of u relax(u,v)

112

slide-113
SLIDE 113

while there exists a white vertex min = infinity for each vertex v if color[v] is white and d[v] < min min = d[v] u = v color[u] = yellow for each neighbor v of u

Array Implementation

113

slide-114
SLIDE 114

while there exists a white vertex u = q.getMin() color[u] = yellow for each neighbor v of u relax(u,v)

Priority Queue Implementation

114

slide-115
SLIDE 115

proc relax (v,w): Let d = d[v] + cost(v,w) if d[w] > d d[w] = d q.decreaseCost(w, d)

Priority Queue Implementation

115

slide-116
SLIDE 116

Summary: Graph

  • Basic terms
  • Representations
  • Applications
  • BFS
  • find shortest path in unweighted path
  • finding connected component

116

slide-117
SLIDE 117

Summary: Graph

  • DFS
  • finding connected component
  • check for cycles
  • topological sort
  • Dijkstra algorithm
  • finding shortest path from a single source in

a weighted graph with positive weights.

117