06.graphs and trees - hope college · 2006-12-31 · graphsandtrees 6 graph denitions summary there...

38
Graphs and Trees 1 Graphs A (simple) graph = consists of , a nonempty set of vertices and , a set of unordered pairs of distinct vertices called edges. Examples B D E C A V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Upload: others

Post on 24-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 1

Graphs� A (simple) graph

�= ������� consists of

– � , a nonempty set of vertices and– � , a set of unordered pairs of distinct vertices

called edges.

Examples

B

D E

CA

V={A,B,C,D,E}E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Page 2: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 2

Directed Graphs� A directed graph (or digraph)

�= ������

consists of– � , a nonempty set of vertices and– � , a set of ordered pairs of distinct vertices

called edges.� Examples

Page 3: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 3

Multigraphs� A multigraph (directed multigraph)� � ������� consists of

– � , a set of vertices,– � , a set of edges, and– a function � from � to � �������� ��� �� � � � �

(function � from � to ��������! "��� �� � � � � .)� Two edges #%$ and #�& with �'�(#)$� � �'�(#�&* are

called multiple edges.� Put simply, a multigraph

� � ������� is a graphin which multiple edges are allowed.

� Examples

Page 4: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 4

Weighted Graphs� A weighted graph is a graph (or digraph) with the

additional property that each edge # has associatedwith it a real number + �,#- called it’s weight.

� A weighted digraph is often called a network.� Examples

70

43

-6

3

12

114

3

-2

30

41

12

4

6

78

53

Page 5: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 5

Psuedographs� A psuedograph

� � �.���� consists of– � , a set of vertices,– � a set of edges, and– a function � from � to /%/�01�3254 6701�32 8 � 4 .

� Psuedo-multigraphs are defined similarly.� Put another way, a psuedograph is a graph in

which we allow loops, that is, edges from a vertexto itself.

Examples

Page 6: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 6

Graph Definitions Summary� There are several ways to categorize graphs:

– Directed or undirected edges.– Weighted or unweighted edges.– Allow multiple edges or not.– Allow loops or not.

� Unless specified, you can usually assume a graphdoes not allow multiple edges and loops. Thesearen’t that common.

� For clarity, if a graph is not specified as weightedor directed, assume it isn’t.

� The most common graphs we’ll use are graphs,digraphs, weighted graphs, and networks.

� When writing graph algorithms, it is important toknow what characteristics the graphs have. Forinstance, if a graph might have loops, thealgorithm should be able to handle it.

Page 7: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 7

Graph Terminology

Let 0 and 2 be vertices, and let # � /90:�;254 be an edgein an undirected graph

�.

� The vertices 0 and 2 are said to be adjacent� The edge # is said to incident with 0 and 2 .� The edge # is said to connect 0 and 2 .� The vertices 0 and 2 are called the endpoints of

the edge # .� The degree of a vertex, denoted <=#?>@�A2B , in an

undirected graph is the number of edges incidentwith it (where loops are counted twice).

uu

uxw

yv

x

w

y

x

v

w

y

z

v

G2 3GG1

z

Page 8: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 8

Examples

uu

uxw

yv

x

w

y

x

v

w

y

z

v

G2 3GG1

z

C D CFE CHGdeg(u)=3 deg(u)=2 deg(u)=2deg(v)=5 deg(v)=3 deg(v)=4deg(w)=3 deg(w)=2 deg(w)=3deg(x)=2 deg(x)=4 deg(x)=2deg(y)=2 deg(y)=3 deg(y)=3deg(z)=3 deg(z)=2

Page 9: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 9

More Graph Terminology� A subgraph of a graph

� � ������ is a graph� IJ� ��� I ��� I such that � I@K � and � I@K � .

H3H2H1� A path is a sequence of vertices 2 $ �;2 & �*LML*LN�;2!O

such that consecutive vertices 27P and 2)PRQ $ areadjacent

d e

c

abecd bedce

ba b

d e

c

a

Page 10: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 10

More Graph Terminology� A simple path is a path with no repeated vertices.

a

becc

ed

b

� A cycle is a simple path whose last vertex is thesame as the first vertex.

a

cdecc

ed

b

Page 11: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 11

More Graph Terminology� A graph is called connected if there is a path

between every pair of distinct vertices.

not connectedconnected

c

e

a b

d

c

ed

ba

� A connected component of a graph is a maximalconnected subgraph. e.g. the graph below has 3connected components.

Page 12: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 12

Trees� A tree (or unrooted tree) is a connected acyclic

graph. That is, a graph with no cycles.� A forest is a collection of trees.

tree

tree

tree

tree

� These trees are not to be confused with rootedtrees, which we will see later.

Page 13: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 13

Spanning Tree� A spanning tree of

�is a subgraph which is a

tree and contains all of the vertices of�

.

G spanning tree of G

Page 14: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 14

Some Special Graphs� S T : The complete graph on U vertices.

KKKK2 3 4 5

� V T : The cycle of length U .

C C C43 5

Page 15: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 15

� W T : The U -cube.

Q Q

Q1

3

2

Q4

Page 16: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 16

� Bipartite Graphs: A simple graph�

is calledbipartite if the vertex set � can be partitionedinto two disjoint nonempty sets � $ and � & suchthat every edge connects a vertex in � $ to a vertexin � & .Put another way, no edges in � $ are connected toeach other, and no edges in � & are connected toeach other.

Page 17: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 17

Some Theorems� Theorem 1: Let

� � ������ be an undirectedgraph with # edges. Then

X # �Y�Z\[ <=#?>@�A2B�L

� Proof: Let ] � /=�,#7�32^_6�# 8 � �32 8���`# and 2 are incident 4 . We will compute ab] a intwo ways. Each edge # 8 � , is incident withexactly 2 vertices. Thus,

ab] a � X #7LAlso, each vertex 2 8 � is incident with deg( 2 )edges. Thus, we have that

ab] a � Y�Zc[ <=#?>d�e2B�L

Setting these equal, we have the result

� Corollary 2: An undirected graph has an evennumber of vertices of odd degree.

Page 18: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 18

Examples

uu

uxw

yv

x

w

y

x

v

w

y

z

v

G2 3GG1

z

Graph� $ � & � f

ag� a 9 7 8

Y�Zc[ <=#?>d�e2^ 18 14 16

Page 19: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 19

Directed Graph TerminologyLet 0 and 2 be vertices in a directed graph

�, and let

# � �A0:�32^ be an edge in�

.� 0 is said to be adjacent to 2 .� 2 is said to be adjacent from 0 .� 0 is called the initial vertex of �A0:�;2B .� 2 is called the terminal or end vertex of �A0:�;2B .� The in-degree of 0 , denoted by <=#?>ih_�A0@ , is the

number of edges in�

which have 0 as theirterminal vertex.

� The out-degree of 0 , denoted by <=#?> Q �e0@ , is thenumber of edges in

�which have 0 as their initial

vertex.� Theorem 3: Let

� � ������ be a directed graph.Then

Y�Zc[ <=#?> h �e2B � Y�Z\[ <=#?> Q �A2B � ag� ajL

Page 20: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs

andTrees

20

Examples

u

v

w xy

zu

v

w

x

yG4 G5u

v w

x

y

6G

kml kon kop

deg

q

(u)=2 deg

r

(u)=4 deg

q

(u)=1 deg

r(u)=0 deg

q(u)=1 deg

r

(u)=1

deg

q

(v)=2 deg

r

(v)=2 deg

q

(v)=1 degr

(v)=2 deg

q

(v)=2 deg

r

(v)=2deg

q

(w)=1 deg

r

(w)=1 deg

q

(w)=1 degr

(w)=1 deg

q

(w)=2 deg

r

(w)=2deg

q

(x)=2 deg

r

(x)=3 deg

q

(x)=1 deg

r(x)=1 deg

q

(x)=1 deg

r

(x)=1

deg

q

(y)=3 deg

r

(y)=0 deg

q(y)=2 deg

r(y)=2 deg

q

(y)=2 deg

r

(y)=2deg

q

(z)=1 deg

r

(z)=1

Page 21: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 21

Graph Representation

There are two common ways of representing�

. Let� � ������� be a graph with U vertices and s edges.

Adjacency Lists� For each vertex 2 in

�, we store a list of vertices

adjacent to 2 .� This is often implemented using linked lists.

a

e

b c d

a ea d ca c eb c d

d

b

d e

c

a

bc

� For weighted graphs, an additional field can bestored in each node.

� The space required for storage ist �AU u X s � t �eU u s for graphs, andt �AU u s � t �eU u s for digraphs.

Page 22: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 22

Adjacency Matrix� Number the vertices v%� X �ML*LML?�;U in some arbitrary

order.� We use a U by U matrix w defined as

w �Ax��.y^ � v if �Ax���y^ is an edgezif �Ax���y^ is not an edge

� If�

is weighted, we store the weights in thematrix. For non-adjacent vertices, we store { , orMAX INT.

� It is clear that this representation requirest �AU &

space.

c

ed

ba

dcba

a b c d e

e 0 1 1 1 01 0 1 0 11 0 0 1 11 0 0 0 10 1 1 1 0

Page 23: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 23

Some Basic Graph Problems� Is there a path from A to B?� CYCLES: Does the graph contain a cycle?� CONNECTIVITY (SPANNING TREE): Is there

a way connect the vertices?� BICONNECTIVITY: Will the graph become

disconnected if one vertex is removed?� PLANARITY: Is there a way to draw the graph

without edges crossing?� SHORTEST PATH: What is the shortest way from

A to B?� LONGEST PATH: What is the longest way from

A to B?� MINIMAL SPANNING TREE: What is the best

way connect the vertices?� TRAVELING SALESMAN: What is the shortest

route to connect the vertices without visiting thesame vertex twice?

Page 24: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 24

(Rooted) Trees� A rooted tree is a tree which has a specially

designated vertex called the root.� In rooted trees, vertices are called nodes.� Each node contains some information and one or

more links to other nodes further down thehierarchy. (Similar to nodes in a linked list.)

� For convenience, we can think of trees as acyclicdigraphs in which every edge “points away from”the root.

D

F G H

C

A

B

I

0

1

2

3

Level

J

K L M

E

Page 25: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 25

Rooted Trees Terminology

D

F G H

C

A

B

I

0

1

2

3

Level

J

K L M

E

� A node that is adjacent from 2 is a child of 2 .� A node that is adjacent to 2 is a parent of 2 .� Two nodes who have the same parent are siblings.� A leaf or external node is a node with degree

zero. (i.e. a node with no children)� An internal node is a nonleaf node. (i.e. a node

with at least one child)� An ancestor of a node is any node on the path

from the root to the node.� A descendant of a node 2 is any node which has

2 as an ancestor.

Page 26: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 26

Rooted Trees Terminology

D

F G H

C

A

B

I

0

1

2

3

Level

J

K L M

E

� The degree of a node is the number of childrenthe node has.

� The depth of a node is the length of the path fromthe root to the node.

� The height of a node is the maximum length of apath from the node to a leaf.

� The height or depth of a tree is the maximumheight of any node in the tree.

� The subtree rooted at | is the subtree consistingof | and all of its descendents.

Page 27: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 27

Binary Tree� A binary tree is a finite set of nodes that is either

empty or consists of a root and two disjoint binarytrees called the left subtree and the right subtree

� Put another way, a binary tree is a rooted treesuch that each node has– no children,– a right child,– a left child, or– both a right child and a left child.

A

B

C

E

D

F G

H I

E

A

B C

D

Page 28: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 28

Binary Trees: Definitions� A full binary tree is one in which internal nodes

completely fill every level, except possibly thelast.

� A complete binary tree is a full binary tree wherethe internal nodes on the bottom level all appearto the left of the external nodes on that level.

� Example: A full binary tree

1

2 3

4 5 6

8 9 11 13 14 15

7

Page 29: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 29

Binary Tree Examples� Example: A complete binary tree

1

2 3

4 5 6 7

8 9 10 11 12

� Example: A totally complete binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 30: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 30

Properties of Binary Trees� The maximum number of nodes on level x of a

binary tree isX P �;x~} v .

� The maximum number of nodes in a binary tree ofdepth � is

X ONQ $�� v7��� } v .� There is exactly one path connecting any two

nodes in a tree� A tree with U nodes has U � v edges� The height of a full binary tree with U internal

nodes is about ���%� & U .

A

B

C

E

D

F G

H I

E

A

B C

D

Page 31: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 31

Binary Tree Representation:Arrays

Let�

be a tree of height ���%� & U with s nodes, wheres � U . We can represent

�with an array � of size U .

� �jvN� is the root, and given a node with index x , we canfind the index of parents and children as follows:

� ���c� #MU����ex� � � x�� X-� if x �� vundefined if x � v

� � #����M�Ax� �X x if

X x�� Uundefined if

X x~� U� � x�>��B���ex� �

X x@u v ifX xdu v � U

undefined ifX x�u v � U

A

B

C

D

E

F

G

H

I

1

2

3

4

5

6

7

8

9

A

B

C

D

E

1

2

3

4

5

8

16

F G

A

B

C

E

D

A

D E

H I

1

2

4

5

3

CB

Page 32: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 32

Binary Tree Representation:Linked Lists

� We define a tree node bystruct treenode{

int data;treenode *left_child;treenode *right_child;};

� We find children by following the pointers.� Parents are harder to find, unless we use doubly

linked lists.

^

^

^

E

D

C

B ^

^

^ IH

G

^

rootroot

^ ^^ F

A

^

^^

^ED

CB

^

A ^

Page 33: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 33

Binary Tree Traversals� When we visit each node in the tree exactly once,

we say we have Traversed the tree.� A full traversal produces a linear order of the

information in a tree.� There are several ways to traverse a tree.

– preorder: visit a node, then traverse its leftsubtree, and then traverse its right subtree.

– inorder: traverse the left subtree, visit thenode and then traverse its right subtree

– postorder: first traverse the left subtree,traverse the right subtree, and then visit thenode.

� There is a natural correspondence between thesetraversals and producing the prefix, infix andpostfix form of an expression.

Page 34: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 34

Tree Traversal� preorder: visit a node, then traverse its left

subtree, and then traverse its right subtree.� inorder: traverse the left subtree, visit the node

and then traverse its right subtree.� postorder: first traverse the left subtree, traverse

the right subtree.

9 10

8

12

161511

13

7

1

2

6

5

4

3 14

C

D

E

Preorder: + * * / A B C D E

nfix form of the expressionA / B * C * D + EInorder:

Postorder: A B / C * D * E +

B

18 19

17

+

A

/

*

*

Page 35: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 35

Tree Traversal: Implementation� Assume we have used a linked list to implement a

tree.struct tree_node{

int data;tree_node *left_child;tree_node *right_child;};

� Assume we have a pointer to the root node.� From this, we can traverse the tree with any of the

methods.

Page 36: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 36

Preorder Traversal

Visit a node, then traverse its left subtree, and thentraverse its right subtree.

void PreOrderTraversal(treenode *ptr) {if (ptr!=0) {

cout << ptr->data;PreOrderTraversal(ptr->left);PreOrderTraversal(ptr->right);}

}

A

B

C

E

D

F G

H I

E

A

B C

D

Page 37: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 37

Postorder Traversal

First traverse the left subtree, traverse the rightsubtree, and finally visit the node.

void PostOrderTraversal(treenode *ptr) {if (ptr!=0) {

PostOrderTraversal(ptr->left);PostOrderTraversal(ptr->right);cout << ptr->data;}

}

A

B

C

E

D

F G

H I

E

A

B C

D

Page 38: 06.graphs and trees - Hope College · 2006-12-31 · GraphsandTrees 6 Graph Denitions Summary There are several ways to categorize graphs: Œ Directed or undirected edges. Œ Weighted

Graphs and Trees 38

Inorder Traversal

Traverse the left subtree, visit the node and thentraverse its right subtree.

void InOrderTraversal(treenode *ptr) {if (ptr!=0) {

InOrderTraversal(ptr->left);cout << ptr->data;InOrderTraversal(ptr->right);}

}

A

B

C

E

D

F G

H I

E

A

B C

D