32
Graphs Slide credits: K. Wayne, Princeton U. 1

graphs - cpe.ku.ac.th

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: graphs - cpe.ku.ac.th

Graphs

Slide credits: ▪ K. Wayne, Princeton U.

1

Page 2: graphs - cpe.ku.ac.th

These are not Graphs

EastWestNorth

...not the kind we mean, anyway

2

Page 3: graphs - cpe.ku.ac.th

These are Graphs

K5 K3,3

=

3

Page 4: graphs - cpe.ku.ac.th

4

Page 5: graphs - cpe.ku.ac.th

Undirected Graphs

Undirected graph. G = (V, E) is an ordered pair consisting of � V = nodes. � E = edges between pairs of nodes. � Captures pairwise relationship between objects. � Graph size parameters: n = |V|, m = |E|. � Two nodes connected by an edge are said to be neighbor.

V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

5

Page 6: graphs - cpe.ku.ac.th

Some Properties of Undirected Graph

� |E|= O(V2)

� If G is connected |E|≥|V|–1

– An undirected graph is connected if for every pair of nodes u and

v, there is a path between u and v.

� deg(V) is defined as the number of edges incident to V

– Handshaking lemma: Σv∈V deg(v) = 2|E|

6

Page 7: graphs - cpe.ku.ac.th

Directed Graphs

Directed graph. G = (V, E) � Edge (u, v) goes from node u to node v.

Ex. Web graph - hyperlink points from one web page to another. � Directedness of graph is crucial. � Modern web search engines exploit hyperlink structure to rank web

pages by importance.

7

Page 8: graphs - cpe.ku.ac.th

World Wide Web

Web graph. � Node: web page. � Edge: hyperlink from one page to another.

cnn.com

cnnsi.comnovell.comnetscape.com timewarner.com

hbo.com

sorpranos.com

8

Page 9: graphs - cpe.ku.ac.th

Paths and Connectivity

Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is joined by an edge in E.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

9

Page 10: graphs - cpe.ku.ac.th

Cycles

Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the first k-1 nodes are all distinct.

cycle C = 1-2-4-5-3-1

10

Page 11: graphs - cpe.ku.ac.th

Trees

Def. An undirected graph is a tree if it is connected and does not contain a cycle.

Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. � G is connected. � G does not contain a cycle. � G has n-1 edges.

11

Page 12: graphs - cpe.ku.ac.th

Rooted Trees

Rooted tree. Given a tree T, choose a root node r and orient each edge away from r.

Importance. Models hierarchical structure.

a tree the same tree, rooted at 1

v

parent of v

child of v

root r

12

Page 13: graphs - cpe.ku.ac.th

13

Page 14: graphs - cpe.ku.ac.th

14

Page 15: graphs - cpe.ku.ac.th

15

Page 16: graphs - cpe.ku.ac.th

16

Page 17: graphs - cpe.ku.ac.th

17

Page 18: graphs - cpe.ku.ac.th

Graph Representation: Adjacency Matrix

Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. � Two representations of each edge. � Space proportional to n2. � Checking if (u, v) is an edge takes O(1) time. � Identifying all edges takes O(n2) time. � Use for dense graph

1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0

18

Page 19: graphs - cpe.ku.ac.th

Graph Representation: Adjacency List

Adjacency list. Node indexed array of lists. � Two representations of each edge. � Space proportional to m + n. � Checking if (u, v) is an edge takes O(deg(u)) time. � Identifying all edges takes O(m + n) time. � Use for sparse graph

1 2 3

2

3

4 2 5

5

6

7 3 8

8

1 3 4 5

1 2 5 87

2 3 4 6

5

degree = number of neighbors of u

3 7

19

Page 20: graphs - cpe.ku.ac.th

Graph Traversal

� Problem: Search for a certain node or traverse all nodes in the graph

� Depth First Search – Once a possible path is found, continue the search until the

end of the path � Breadth First Search

– Start several paths at a time, and advance in each one step at a time

20

Page 21: graphs - cpe.ku.ac.th

Depth First Traversal

� A natural way to do Depth-first search (BFS) is using recursion.

DFS( Node c ) { Mark c "Visited” For each neighbor n of c If n "Unvisited" DFS( n ) }

Possible visit sequence: 1, 2, 4, 5, 6, 3, 8, 7

21

Page 22: graphs - cpe.ku.ac.th

Breadth First Traversal

� Breadth-first search (BFS) not naturally recursive. � Use a queue so that vertices are visited in order according to

their distance from the starting vertex.

BFS(Node v) { create a queue Q enqueue v onto Q mark v “Visited” while Q is not empty { dequeue t from Q for each neighbor u of t do if u is not marked { mark u “Visited” enqueue u onto Q } }

22

Page 23: graphs - cpe.ku.ac.th

Breadth First Search

L0

L1

L2

L3

Visit sequence: 1, 2, 3, 4, 5, 7, 8, 6

23

Page 24: graphs - cpe.ku.ac.th

Applications: Finding a Path

� Find path from source vertex s to destination vertex d

� Use graph search starting at s and terminating as soon as we reach d

� Need to remember edges traversed

� Use depth – first search

24

Page 25: graphs - cpe.ku.ac.th

DFS

EF

G

B

CD

A start

destination

A DFS on A ADFS on BB

A

DFS on CBC

AB Return to call on BD Call DFS on D

ABD

Call DFS on GG found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G

DFS Process

25

Page 26: graphs - cpe.ku.ac.th

26

Page 27: graphs - cpe.ku.ac.th

27

Page 28: graphs - cpe.ku.ac.th

28

Page 29: graphs - cpe.ku.ac.th

29

Page 30: graphs - cpe.ku.ac.th

30

Page 31: graphs - cpe.ku.ac.th

31

Page 32: graphs - cpe.ku.ac.th

What We Have Learned

� Graph abstraction capturing objects and their relationships

� Graph representation with adjacency matrix and adjacently list

� Operations on graphs:

� BFS

� DFS

� Applications of BFS and DFS

32