computing fundamentals 2 lecture 1 a theory of graphs lecturer: patrick browne room [ka] - 3-020,...

116
Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach to Discrete Math By David Gries and Fred B. Schneider http://www.comp.dit.ie/pbrowne/compfund2/ compfun2.htm

Post on 18-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Computing Fundamentals 2Lecture 1

A Theory of Graphs

Lecturer: Patrick BrowneRoom [KA] - 3-020, Lab [KA] - 1-017

Based on Chapter 19. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

http://www.comp.dit.ie/pbrowne/compfund2/compfun2.htm

Page 2: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of Graphs

• We all have an intuitive understanding of graphs.

Page 3: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of GraphsQuestion: Starting at Newport is it possible to travel each road exactly once and return to Newport?

Page 4: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Some terminology

• In graph theory terms, the dots on the map are called vertices and the lines that connect the vertices are called edges. We have labelled the edges e1..e13. If we travel from Newport to Cork we call this a path from Newport to Cork.

• The graph can be written as: G = <V,E>• V = {Newport, Sligo, Athlone, Drogheda, Dublin,

Carlow, Waterford, Cork, Limerick, Galway}• E = {e1, e2, … e13}

• This is an undirected graph, there are no one-way streets.

Page 5: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Rephrasing the question

• Question: Starting at Newport is it possible to travel each road exactly once and return to Newport?

• Our problem can be restated:– Is there a path from vertex Newport to vertex

Newport that traverses each edge exactly once?

Page 6: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Reasoning about the question

• Suppose there was such a path consider the vertex Limerick. Each time we arrive at Limerick on some edge , we must leave using a different edge. Furthermore, every edge that touches Limerick must be used.

Page 7: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Reasoning about the question

• Thus the edges at Limerick occur in in/out pairs. It follows that an even number of edges must touch Limerick. Since three edges touch Limerick (e6,e9,e11) we have a contraction. Therefore, there is no path from Newport to Newport that traverses every edge once. We have proved this using graph theory.

Page 8: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Algorithms in proofs

• An algorithm is a finite list of well-defined instructions for accomplishing some task that, given an initial state, will proceed through a well-defined series of successive states, possibly eventually terminating in an end-state1.

• The proof of some results in graph theory may involve an algorithm to construct some object like a path from one vertex to another. In Chapter 19 of Gries and Schneider algorithms are used in proofs2

Page 9: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Graphs and Multigraphs

• (19.1) Definition: Let V be a finite, non empty set and let E be a binary relation on V. Then G=<V,E> is called a directed graph or a digraph. An element of V is called a vertex; an element of E is called an edge.

• The binary relation E is defined as EV×V.• In figure 19.1 E and V are: • E = {<b,b>,<b,c>,<b,d>,<c,e>,<e,c>,<e,d>}• V = {a,b,c,d.e}

• A digraph G=<V,E> is loop free iff E is an irreflexive relation (no <b,b> ).

Page 10: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

V = {a,b,c,d.e}

E = {<b,b>,<b,c>,<b,d>,<c,e>,<e,c>,<e,d>}

A directed graph or digraph c

a

d

b

e

d

c

b

a d

c

b

a

An undirected graph represented as a digraph

Fig. 19.1

Fig. 19.2bFig. 19.2 a

Here E is a set of unordered pairs i.e {a,b} rather than <a,b>. {a,b}, {a,c}, {b,c},

{b,d} these are sets{a,b} could be written <a,b>, <b,a>.

There is a one-to-one correspondence between undirected graphs and digraphs with symmetric relations.

Page 11: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Aside: Bags

• The elements of a set are distinct. Sometimes we need to allow duplicates. For example, if we wanted to count the people in Dublin using a list of their names we would have to allow for duplicate names, a set would not be appropriate. Mathematically a bag is a set with duplicates therefore many of the familiar set operations apply to bags.

Page 12: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Graphs and Multigraphs

• A multigraph is a pair <V,E>, where V is a set of vertices and E is a bag of undirected edges.

• In a multigraph there can be many edges between two given vertices. Every undirected graph is a multigraph (but not visa versa).

• Multigraphs do not depict relations.• In order to distinguish edges in a multigraph we

can label the edges.

Page 13: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Graphs and Multigraphs

Euler’s 7 bridges of Konigsberg, representing bridges as edges, land as vertices.

               →                →               

Because a multigraph consists of a bag of undirected edges we need labels to distinguish edges. This means that they may be parallel edges between two given vertices.

1

2

3

45

6

7

Page 14: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

The Degree of A Vertex and Graph

• The indegree of a vertex of a digraph is the number of edges for which it is an end vertex.

• The outdegree is the number of edges for which it is a start vertex.

• The degree of a vertex, written deg.v, is the sum of the indegree and the outdegree.

• A vertex whose degree is 0 is called isolated.• The degree of a vertex of an undirected graph or

multigraph is the number of edge ends attached to it.

Page 15: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

The Degree of A Vertex and Graph

• Each edge contributes 1 to the degree of two vertices (which in a loop may be the same vertex, thus adds two to that vertex).

• (19.3) Theorem. The sum of the degrees of the vertices of a digraph or multigraph is 2 * #E (E is a set, see next slide).

• (19.4) Corollary. In a digraph or a multigraph the number of vertices of odd degree is even.

Page 16: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

The Handshaking Theorem

Theorem: Let G=(V,E) be an undirected graph. Then twice the number of edges is equal to the sum of the degrees of V.

Number of edges = 7

1 + 2 + 3 + 2 + 2 + 3 + 1 = 14

In any group of people the number of people who have shaken hands with an odd number of other people from the group is even. Zero is an even number

Page 17: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Even degree

• Loop edge gives vertex a degree of 2.• In any graph the sum of degrees of all vertices equals

twice the number of edges.• The total degree of a graph is even.• In any graph there are even number of vertices of odd

degree.

Page 18: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Paths• The land masses are modelled as vertices. The bridges

are modelled as edges. Is it possible to tour Königsberg in a way that would traverse each bridge (edge) exactly once?

Deg(d) = 5Deg(a) = 3Deg(b) = 3Deg(c) = 3--------------- 14#Edges = 7(2 x #Edges) = 14

Fig. 19.3(b)

Page 19: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Aside: A theory of Sequences

• A sequence is a finite list of elements from some set. The theory of sequences can be used to reason about graphs, lists and arrays.

• Sequences are ordered, but not necessarily sorted. Like runners in a race there is a first, second and third runner. Their finishing position in the race does not usually depend on their race number. At the finish runners are not (usually!) sorted by race number.

Page 20: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Aside: A theory of Sequences

• Catenation: When we join two sequences together we want to preserve the position of each element. This can be illustrated by using the CafeOBJ sequence module.

• red [1 , 2 ] ^ [ 1, 2, 3 , 4 ] ^ [] ^ [0]

• Giving

• [1, 2, 1, 2, 3, 4, 0]• Note that duplicates are allowed.

Page 21: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Path

• A path of a multigraph or a digraph is a sequence of vertices and edges that would be traversed when walking the graph from one vertex to another, following the edges, but with no edge traversed more than once. In a directed graph an edge can only be traversed in one direction.

Page 22: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Paths : Example

• A path that starts at vertex b and ends at vertex c is called a b-c path. Here represented as a sequence of vertices and edges.

c

a

d

b

e

Fig. 19.1

• Path1: <c,<c,e>,e,<e,d>,d>

• Path2: <b,<b,c>,c,<c,e>,e,<e,c>,c> • Non-Path: <b,<b,c>,c,<c,e>,e,<e,c>,c,<c,e>,e>

Page 23: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Path length, alternative notation

Fig. 19.3(b)

The length of a path is the number of edges in it.

Path3 : <a,6,b,5,d,1,a> Length = 3 edges

Page 24: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Path intermediate summary

1. A path starts with a vertex, ends and alternates between vertices and edges.

2. Each directed edge in a path is preceded by its start vertex and followed by its end vertex. An undirected edge is preceded by one of its vertices followed by the other.

3. No edge appears more than once.

4. The length of a path is the number of edges on it. The path with just one vertex <a> has length zero. A self loop <b,b> is a cycle of length one.

Page 25: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Redundant Notation

• The notation (<a,6,b,5,d,1,a>) can be used for all graphs (directed, undirected and multigraphs).

• While the notation is necessary for multigraphs it is redundant for directed and undirected graphs (even though they are simple by 2 multigraphs!).

• In a digraph, for any two vertices b and c, there is at most one edge from b to c.

• Similarly, in any undirected graph, there is at most one edge between two given vertices.

• No need to show edges for directed and undirected graphs, hence;

• Path1 = <c,e,d>. – just vertices• But labelling is required for multigraphs.

Page 26: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Simple Path

• A simple path is a path in which no vertex appears more than once (first and last may be same). Examples from Fig. 19.1:

• <a> an isolated node, a path with no edges,len=0

• <b,b> loop, first=last => cycle, length=1• <b,c,e,d>• <b,c,e,c> not simple path

Page 27: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

An Example of a Simple-Path • In the following diagram the red edges show a

simple path of length 3 from a to e. The simple path is acde of length 3. We could also have a non-simple path acdabde of length 6.

• A simple path: no vertex appears more than once.

a

b c

d e

Page 28: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

X

by

d

Z

cUses sequence notation

Page 29: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach
Page 30: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Cycle

• A path with at least one edge and with the first and last vertices the same is called a cycle.

• (19.7) Theorem: Suppose all the vertices of a loop free multigraph have even degree. Suppose deg.b > 0 for some vertex b. Then some cycle contains b. Note, zero is an even number and is neither positive nor negative.

• Note that unlike paths, any vertex of a cycle can be chosen as the start, so the start is sometimes not specified.

Page 31: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Connected Graph

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

• A digraph is connected if making its edges undirected results in a connected multigraph.

Page 32: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

digraph: not connected c

a

d

b

e

Fig. 19.1

d

c

b

a

Fig. 19.2 a

Connected graphs

•The graph in Fig 19.1 is not connected, the graphs in Fig 19.2a and Fig 19.3b are connected.

Page 33: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Connected Directed Graph1

•   There are three distinct forms of connectedness in simple directed graphs: weakly connected, unilaterally connected and strongly connected. 

A directed graph is strongly connected if every two vertices are reachable from each other. Unilaterally connected graphs need the concept of semi-path, which we do not cover.

Page 34: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

1 7

65

4

3

2

Path Simple Path? Cycle? Simple Cycle?

<6,5,2,4,3,2,1> no no no

<6,5,2,4> yes no no

<2,6,5,2,4,3,2> no yes no

<5,6,2,5> yes(book def) yes yes

<7> yes no no (length=0)

We have a path when no edge traversed more than once.A simple path from. v to w is a path from v to w with no repeated vertices. (first & last may be the same)<v0,e1,v1,e2,v2,…,vn-1,en,vn> where v0=v and vn=w

A cycle (or circuit) is a path of nonzero length from v to v with no repeated edges Must return to start.A simple cycle is a cycle is a non-zero sequence of vertices and edges, from v to v in which, except for the beginning and ending vertices that are both equal to v, there are no repeated vertices.

Page 35: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Graph Representation 1

• Adjacency matrix• Rows and columns

are labeled with ordered vertices write a 1 if there is an edge between the row vertex and the column vertex and 0 if no edge exists between them

v w x y

v 0 1 0 1

w 1 0 1 1

x 0 1 0 1

y 1 1 1 0

Page 36: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Graph Representation 2

• Incidence matrix– Label rows with vertices– Label columns with edges– 1 if an edge is incident to

a vertex, 0 otherwise

e f g h j

v 1 1 0 0 0

w 1 0 1 0 1

x 0 0 0 1 1

y 0 1 1 1 0

Page 37: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Three applications of Graphs

• The Konigsberg Bridge Problem

• The Celebrity Problem

• Route finding

• The states of river crossing puzzle (slides towards end of this lecture).

Page 38: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Celebrity problem uses a directed graph

• At a party, a celebrity is a person who everyone knows and who knows no one (except themselves).

• In graph theory terms what would be represented by nodes and vertices?

• Would the graph be directed or undirected?

Page 39: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Definition & Application of Euler Cycle

• An Euler cycle is a cycle in a graph that includes each edge exactly once. Examples: Designing and optimizing routes for refuse trucks, snow ploughs, or postmen. In all of these applications, every edge in a graph must be traversed at least once. For example, roads in a city must be completely traversed at least once in order to ensure that all rubbish is collected, all snow cleared, all post delivered. We seek to minimize total time, or the total distance or number of edges traversed. It may not always be possible to travel each edge exactly once and a near optimal solution may have to be used.

Page 40: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• An Euler path in a multigraph is a path that contains each edge exactly once. If the first and last vertices are the same then the Euler path is called an Euler cycle (or circuit). Example: G1 has Euler path from a to b

• a, c, d, e, b, d, a, b

a b

c d e

Graph G1

Page 41: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c

a b

c d e

Page 42: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c, d

a b

c d e

Page 43: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c, d, e

a b

c d e

Page 44: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c, d, e, b

a b

c d e

Page 45: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c, d, e, b, d

a b

c d e

Page 46: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a, c, d, e, b, d, a

a b

c d e

Page 47: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Path

• a , c, d, e, b, d, a, b

a b

c d e

Page 48: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 49: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 50: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 51: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 52: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 53: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 54: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 55: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 56: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 57: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 58: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 59: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 60: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 61: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 62: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 63: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 64: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 65: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 66: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 67: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 68: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycle

a b

c

d e

f g

ml

hk

on

j

i

Euler cycle: a,d,c,i,j,n,o,k,j,d,e,k,l,h,m,g,h,f,e,b,a

Page 69: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Euler Cycles and Paths

Has Euler cycle

Have Euler paths,but no Euler cycleFind Euler Paths. No repeating edgesIs Euler path unique?

No Euler cycleno Euler path

a b

c d e

Page 70: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

The general theory from the Konigsberg Bridge Problem

• (19.8) Theorem. An undirected connected multigraph has an Euler circuit iff every vertex has even degree.

• Euler Circuit = Even Degree

• The proof is in two parts

• (1) LHS => RHS and (2) RHS => LHS

• This is called proof by mutual implication.

Page 71: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Example Proof

• Euler Circuit (EC)= Even Degree (ED)• (1) LHS => RHS • This says: Euler Circuit => is of even degree.• Assume that the multigraph has an EC as below:

v0 v1e0

v2e1

vnen

• Each vertex vi on the EC is preceded by an edge, and followed by an edge, and each of the two edges contributes 1 to the degree of vi. Since all the edges appear in the circuit the degree of each vi is even.

Page 72: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Proof(2)

• Euler Circuit(EC)= Even Degree(ED)• (2) RHS => LHS• Long proof. We will not cover the second part of

this proof. If interested, see course text.

Page 73: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Simple Graph

• A graph with neither loops nor parallel edges is called a simple graph (we are talking about graphs not paths).

The graph G1 is a simple graph.

The graph G2 is not a simple graph

G1G2

Page 74: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Complete Graph

• A complete graph on n vertices, Kn, is the simple graph (no loops or parallel edges) with n vertices in which there is an edge between every pair of distinct vertices. In general, the complete graph, Kn, is a simple graph with n vertices, each of which has degree n − 1.

K4

Example K4 n vertices and n(n − 1) / 2 edges4*(4-1)/2 = 6 edges

In any graph, the number of vertices with odd degree is even. In K4 each vertex is of

odd degree: 3+3+3+3=12 = 2 * 6 = 2|E|

Page 75: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Some Complete graphs with number of edges.

K1:0 K2:1 K3:3 K4:6

                                               

K5:10 K6:15 K7:21 K8:28

                

  

                

  

                

  

                

  

Page 76: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

• A graph G=<V,E> is bipartite if there exists subsets V1 and V2 (either possibly empty) of V such that V1V2 = , V1V2 = V (i.e. a partition), and each edge in E is incident on one vertex in V1 and one vertex in V2 . A bipartite graph is a graph that does not contain any odd-length cycles.

v1

A bipartite graph

V1 ={v1 , v2 , v3}

V2 ={ v4, v5 ,}

V1V2 = ,

V1V2 =V

v2

v4

v3

v5

V1 V2

Bipartite Graph

Page 77: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

• A graph G=<V,E> is bipartite if there exists subsets V1 and V2 (either possibly empty) of V such that V1V2 = , V1V2 = V (i.e. a partition), and each edge in E is incident on one vertex in V1 and one vertex in V2 . A bipartite graph is a graph that does not contain any odd-length cycles.

Bipartite Graph

V1 V2

Page 78: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Complete Bipartite Graph• The complete bipartite on m and n vertices Km,n , is

the simple graph whose vertex set are partitioned into two sets V1 with m vertices and V2 with n vertices in which there is an edge between each pair of vertices, where v1 is in V1 and v2 is in V2 .

A complete bipartite graph K2,4

V1 V2

Page 79: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Application of bipartite graphs

• Bipartite graphs can be used to represent relations.

• Bipartite graphs can be used for mapping workers to possible jobs.

• Bipartite graphs are used in Petri nets to model processes. Nodes represent transitions (events-bars) and places (conditions-circles). The directed arcs describe which places are conditions for which transitions (arrows).

Page 80: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Application of bipartite graphs

The graph K3,3 is called the utility graph. This usage comes from a standard mathematical puzzle in which three utilities must each be connected to three buildings; it is impossible to solve without crossings due to the non-planarity of K3,3.

Page 81: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Finding Bipartite graph

• Use two colours say blue & red. Such a colouring of the vertices of a bipartite graph means that the graph can be drawn with the red vertices on the left and the blue vertices on the right such that all edges go from left to right. Colour the first vertex blue, and then do a depth-first search of the graph. Whenever we discover a new, uncoloured vertex, colour it opposite that of its parent, since the same colour would cause a clash. If we ever find an edge where both vertices have been coloured identically, then the graph cannot be bipartite. Otherwise, this colouring will be a 2-colouring and can be constructed.

Page 82: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

The edges are traversed in the order specified by the numbers above.

We will cover graph traversal and searching in the next lecture

Original Graph DFS

Depth First Search (DFS)

1

2

3

4

56

7

Page 83: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Example

• G1 is bipartite because it can be two coloured, blue for the centre and red for the rest.

• G2 (K3) is not bipartite because it cannot be two coloured. If we make V1 red then V2 and V3 must be blue, but V2 is connected to V3. Hence, blue to blue which is not allowed. It will be the same no matter where we start.

3

Page 84: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Finding a bipartition using parity

0 1

12

34

4

45

Odd parity

Even parity

Page 85: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Example

6

4

13

2

5

87

6

41

3

2

8

7

5

The graph is bipartite because every edge goes between an odd-numbered vertex (red) and an even numbered vertex (blue), giving

V = {1,3,5,7} U = {2,4,6,8}

Page 86: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Example

6

41

32

5

1

2

65

3

4

Page 87: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

K-colouring graphs

Page 88: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

K-colouring graphs

A k-colouring partitions of the vertex set into k independent sets.

Page 89: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Sub-graph

e8

e10 e11

e5

v2

e8

e10 e11

e5

v2

Some subgraphs of G1

Graph G1

Page 90: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Sub-graph

Graph G The four possible subgraph G

v2

v1v1

e1

v2 v2

v1

v2

v1

e1

Graph G G1 G2 G3 G4

Page 91: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Graph

• (19.10) Theorem. A path of a bipartite graph is of even length iff its ends are in the same partition.

v1

v2

v4

v3

v5

V1 V2

Page 92: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Graph

• (19.11) Corollary. A connected graph is bipartite iff every cycle has even length e.g. look at the cycle <v3,v5,v2,v4,v3>.

v1

v2

v4

v3

v5

V1 V2

Page 93: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Bipartite Graph

• Another example of (19.11)

V1 V2

Page 94: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Complete Bipartite Graphs

K1,3 K2,3 K3,3

Page 95: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Example

Has the following graph got a Euler cycle. If it has what is it.

Solution: No Euler Cycle, vertex b has odd degree.

a

b

c d

g f

e

In general if you should test for existence, before trying to find Euler cycle

Page 96: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, all vertices of even degree.

v1 v2

v4

v3

v5

Page 97: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3>

v1 v2

v4

v3

v5

Page 98: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5>

v1 v2

v4

v3

v5

Page 99: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2>

v1 v2

v4

v3

v5

Page 100: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4>

v1 v2

v4

v3

v5

Page 101: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5>

v1 v2

v4

v3

v5

Page 102: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5,v1>

v1 v2

v4

v3

v5

Page 103: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5,v1,v4>

v1 v2

v4

v3

v5

Page 104: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5,v1,v4,v3>

v1 v2

v4

v3

v5

Page 105: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5,v1,v4,v3,v2>

v1 v2

v4

v3

v5

Page 106: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

ExampleHas the following graph got a Euler cycle? If it has what is it.

Solution: Yes, <v1,v3,v5,v2,v4,v5,v1,v4,v3,v2,v1>

v1 v2

v4

v3

v5

Page 107: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Example

• Draw a graph with 4 edges and 4 vertices ,• having degrees 1,2,3,4.

• No such graph exists, because Handshaking Theorem(HT) says

• 2 * numberOfEdges(E) = sum(deg(V))• but• 2 * 4 =/= 1+2+3+4• Which is not consistent with HT

Page 108: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

c

a

d

b

e

Fig. 19.1

Sequence Path? Simple Path? Cycle? Simple Cycle?

<c,e,d>

<b,c,e,c,e>

<e,c,e>

<b,b>

<b>

Page 109: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of Graphs1

Page 110: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of Graphs

mod* GRAPH { [ Vertex Edge ]

-- Notation from Chapter 19 Gries and Schneider-- We have two operations called s & t on edges.-- Both operations take an Edge and return a Vertex-- which can be either source or target op <_,_> : Vertex Vertex -> Edge op s : Edge -> Vertex op t : Edge -> Vertex

Page 111: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of Graphs -- General graph equations vars a b : Vertex eq s(< a , b >) = a . eq t(< a , b >) = b . -- A particular instance of graph ops e1 e2 e3 : -> Edge ops v1 v2 v3 : -> Vertex eq e1 = < v1 , v2 > . eq e2 = < v2 , v3 > . eq e3 = < v3 , v1 > . }

Page 112: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A Theory of Graphs

-- v1---e1---v2----e2---v3-- | | -- ----------e3--------------open GRAPH-- What is the start of e1 ?red s(e1) . v1-- Is e1 connect to e2?red t(e1) == s(e2) . true

Page 113: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

A directed graph or digraph G1 c

a

d

b

e

d

c

b

a d

c

b

a

An undirected graph represented as a digraph

Fig. 19.1

Fig. 19.2bFig. 19.2 a

Page 114: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Sample Graph g2v1

v4

v3

v2v5

Graph g2

Page 115: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

Sample Graph g3

v1

v4

v3

v2v5

Graph g2

v7

v6

Page 116: Computing Fundamentals 2 Lecture 1 A Theory of Graphs Lecturer: Patrick Browne Room [KA] - 3-020, Lab [KA] - 1-017 Based on Chapter 19. A Logical approach

a

Sample Graph Path

n1n2

d

n5

Graph with paths see CafeOBJ specification.

f

n3

b

c n4

e