cs1022 computer programming & principles lecture 7.1 graphs (1)

23
CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Upload: desirae-landing

Post on 31-Mar-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

CS1022 Computer Programming &

Principles

Lecture 7.1Graphs (1)

Page 2: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Plan of lecture• What we mean by “graphs”• How it started• Terminology & result• Simple graphs• Adjacency matrix• Subgraphs• Paths and cycles• Acyclic and connected graphs• Connectivity algorithm

2CS1022

Page 3: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

What we mean by “graphs”• Graphs: very specific & special mathematical model– Not “graph” as in a plot of a function using x-y axes

• Is the term familiar?– We’ve used them to represent relations– We did not define them formally

3CS1022

1

5

6

2

3

4

Page 4: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Origins of graphs (1)• Leonhard Euler (18th Century mathematician)• Studied and solved the Königsberg bridge problem– Königsberg is now Kaliningrad (Russia)– It consisted of 2 islands in a river with 7 bridges

• Problem: 1. Find a route starting and ending at the same place2. Traversing all bridges exactly once

4CS1022Leonhard Euler

Page 5: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Origins of graphs (2)

• Euler modelled the problem using a graph– Vertices (or nodes) represent parts of the city– Edges (or links) represent bridges

• He proved that there was no such route

5CS1022

Page 6: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Graphs and computing• Many real-life problems can be modelled as graphs– Networks, web sites, memory management, etc.

• If the problem is modelled as a graph then we can use existing solutions– Find a path between two nodes in a graph– Find a node which all paths go through– Etc.

• We can also rely on many important results– Certain graph problems only solved in exponential time– Only small instances can be solved

6CS1022

Page 7: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Terminology (1)• A graph consists on – A set of vertices (nodes)– A set of edges (links)

• The Königsberg problem– Vertices A, B, C, D (places)– Edges a, b, c, d, e, f, g (bridges)– Start/end on a vertex using each edge exactly once

• Eulerian graph:– Has a route beginning and ending on a vertex– The route uses all edges exactly once

• Eulerian trail:– Is the route itself – Vertices may be repeated (but not the edges)

7CS1022

A

B

C

D

a b

cd

e

f

g

Page 8: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Euler’s result (1)• Euler noticed that if a graph is Eulerian then – Each time an edge is used to travel to some vertex– A different edge would be required to leave that vertex

• This applies no matter how many times a particular vertex needed to be visited

• Hence, if an Eulerian trail exists, then there must be an even number of edges meeting at each vertex

• Euler also proved the converse:– If there is an even number of edges meeting at each

vertex then there is an Eulerian trail

8CS1022

Page 9: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Euler’s result (2)• So the result is– A graph in which every pair of vertices is joined by some

route is Eulerian if, and only if, all the vertices have even degree

• The degree of a vertex, denoted as (v), is the number of edges incident to v.

• There is no such route in the Königsberg graph– (B) (C) (B) 3, (A) 5– There are vertices (all of them!) of odd degree

9CS1022

Page 10: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Euler’s result (3)• Is there a lesson to learn here?– We thought we needed to compute routes, etc. but the

problem can be solved by checking graph properties• We can now write a program which

1. Takes as input a graph2. Checks the property • It’s a simpler problem than computing all paths/routes, starting

from all nodes and back

3. Outputs• Yes, it is an Eulerian graph• No, it is not an Eulerian graph

10CS1022

Page 11: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Simple graphs (1)• A simple graph G is the pair (V, E) where– V is a finite set of vertices and– E is a finite set of edges

Such that G contains – No loops (no vertex is joined to itself by an edge)– No multiple edges (at most 1 edge joining two vertices)

• Königsberg graph is not simple:– Two edges connecting A and B– (Other multiple edges too)

11CS1022

Page 12: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Simple graphs (2)• In simple graphs, vertices u and v are adjacent if

they are connected by an edge e – Edge e is incident to u and v

• Thus the set of edges E can be regarded as a set of pairs of adjacent vertices– E is an irreflexive, symmetric relation on V– Irreflexive: there are no loops in simple graphs– Symmetric: an edge from u to v, also connects v to u • Edges are not directed – these are not digraphs!

• uv (or vu) represents unique edge from u to v

12CS1022

Page 13: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Adjacency matrix• The logical matrix M of the edge relation is called

adjacency matrix• Irreflexive and symmetric properties mean that – Adjacency matrix is symmetric about leading diagonal– Leading diagonal is all Fs

13CS1022

v1 v2 v3 v4

v1 F T F Tv2 T F T Tv3 F T F Fv4 T T F F

Page 14: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

ExampleSuppose G (V, E)• V a, b, c, d, e• E ab, ae, bc, bd, ce, deDraw simple graph Show adjacency matrix

14CS1022

a b c d ea Fb Fc Fd Fe F

a

b

e

c

d

a b c d ea F Tb T Fc Fd Fe F

a b c d ea F T Tb T Fc Fd Fe T F

a b c d ea F T Tb T F Tc T Fd Fe T F

a b c d ea F T Tb T F T Tc T Fd T Fe T F

a b c d ea F T Tb T F T Tc T F Td T Fe T T F

a b c d ea F T Tb T F T Tc T F Td T F Te T T T F

a b c d ea F T F F Tb T F T T Fc F T F F Td F T F F Te T F T T F

a b c d ea F T F F Tb T F T T Fc F T F F Td F T F F Te T F T T F

Page 15: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Subgraph• A subgraph of G (V, E) is any graph G (V, E)

such that V V and E E– That is, a subgraph has a subset of vertices and edges – N.B.: if removing vertices, then edges may need to go

15CS1022

Page 16: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Paths• A path of length k in a graph is a sequence of

distinct vertices v0, v1, , vk such that vi–1vi is an edge, 1 i k

• We denote this path as v0 v1 vk • Example: – a b c e d is a path– b a e c is a path– d e c b a is a path– b e a is not a path– c e a b c is not a path

16CS1022

a

b

e

c

d

Page 17: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Cycles• A cycle in a graph is a sequence of vertices v0, v1, ,

vk such that – vi–1vi is an edge, 1 i k (that is, it is a path)

– v0 vk (first and last vertices are the same)

– vi–1vi, vi–1 vi, 2 i k (other vertices are all distinct)

• Example: – a b c e a is a cycle– b a e c b is a cycle– d e c b d is a cycle

17CS1022

a

b

e

c

d

Page 18: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Acyclic and connected graphs• A graph for which we cannot find cycles is called an

acyclic graph• A graph is connected if there is a path between

every pair of vertices• Example of graph acyclic and connected:

18CS1022

a

b

e

c

d

Page 19: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Connectivity number• Any graph can be partitioned into subgraphs, each

of which is connected• Minimal number of connected subgraphs is called

the connectivity number, c(G)– Connectivity issues are important in the study of

computer networks

19CS1022

Page 20: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Connectivity algorithm (1)• The algorithm below computes the value c(G)

20CS1022

input graph G consisting of pair (V, E) begin

V:= V;c := 0;while V dobegin

Choose y V;Find all vertices joined to y by some path;Remove these vertices and y from V and

the corresponding edges from E;c := c 1;

endoutput c

end

Page 21: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Connectivity algorithm (2)• Trace algorithm with input

21CS1022

input graph G consisting of pair (V, E) begin

V:= V;c := 0;while V dobegin

Choose y V;Find all vertices joined to y by some path;Remove these vertices and y from V and

the corresponding edges from E;c := c 1;

endoutput c

end

1 5

6

7

8

2

3

4

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1 2, 4, 5, 7 1

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1 2, 4, 5, 7 1Choose y = 2

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1 2, 4, 5, 7 1Choose y = 2

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1 2, 4, 5, 7 1Choose y = 2 {7} 2

V cInitial values 1, 2, 3, 4, 5, 6, 7, 8 0Choose y = 1 2, 4, 5, 7 1Choose y = 2 {7} 2Choose y = 7 3

Page 22: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Connectivity algorithm (3)• Therefore c(G) 3• The 3 connected components are:

• N.B.: algorithm does not compute these!

22CS1022

7

5

2

4

1

6

8

3

Page 23: CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Further reading• R. Haggarty. “Discrete Mathematics for

Computing”. Pearson Education Ltd. 2002. (Chapter 7)

• Wikipedia’s entry on graph theory• Wikibooks entry on graph theory

23CS1022