html5-img
1 / 116

Computing Fundamentals 2 Lecture 1 A Theory of Graphs

Computing Fundamentals 2 Lecture 1 A Theory of Graphs. Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Lecture Room K408, Labs A305 & A306 Based on Chapter 19. A Logical approach to Discrete Math By David Gries and Fred B. Schneider. A Theory of Graphs.

faye
Télécharger la présentation

Computing Fundamentals 2 Lecture 1 A Theory of Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing Fundamentals 2Lecture 1A Theory of Graphs Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Lecture Room K408, Labs A305 & A306 Based on Chapter 19. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

  2. A Theory of Graphs • We all have an intuitive understanding of graphs.

  3. A Theory of Graphs Question: Starting at Newport is it possible to travel each road exactly once and return to Newport?

  4. 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.

  5. 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?

  6. 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.

  7. 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.

  8. 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

  9. 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 EV×V. • In figure 19.1 E is • E = {<b,b>,<b,c>,<b,d>,<c,e>,<e,c>,<e,d>} • A digraph G=<V,E> is loop free iff E is an irreflexive relation (no <b,b> ).

  10. A directed graph or digraph c e a b E = {<b,b>,<b,c>,<b,d>,<c,e>,<e,c>,<e,d>} d Fig. 19.1 An undirected graph represented as a digraph b d b a d Here E is a set of unordered pairs i.e {a,b} rather than <a,b>. a {a,b}, {a,c}, {b,c}, {b,d} these are sets c {a,b} could be written <a,b>, <b,a>. c Fig. 19.2b Fig. 19.2 a

  11. 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.

  12. 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.

  13. 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.

  14. 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. • There is a one-to-one relation between undirected graphs and digraphs. • 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.

  15. Graphs and Multigraphs Euler’s 7 bridges of Konigsberg, representing bridges as edges, land as vertices.               →               → 6 3 1 5 2 4 7 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.

  16. 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.

  17. 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.

  18. 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

  19. 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

  20. 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) = 5 Deg(a) = 3 Deg(b) = 3 Deg(c) = 3 --------------- 14 #Edges = 7 (2 x #Edges) = 14 Fig. 19.3(b)

  21. 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.

  22. 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 e a b Fig. 19.1 d • 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>

  23. Paths alternative notation Path3 : <a,6,b,5,d,1,a> Length = 3 edges Fig. 19.3(b)

  24. Path intermediate summary • A path starts with a vertex, ends and alternates between vertices • 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. • No edge appears more than once. • The length of a path is the number of edges on it. The path with just one vertex <a> has length 0.

  25. 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

  26. Simple Path • A simplepath 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

  27. 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 c b d e

  28. d X b Z c y

  29. Cycle • Apathwith 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.

  30. 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.

  31. digraph: not connected c e a b Fig. 19.1 d Connectedgraphs b d a c Fig. 19.2 a • The graph in Fig 19.1 is not connected, the graphs in Fig 19.2a and Fig 19.3b are connected.

  32. 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.

  33. 3 2 4 1 7 6 5 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 edgesMust 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.

  34. 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

  35. Incidence matrix Label rows with vertices Label columns with edges 1 if an edge is incident to a vertex, 0 otherwise Graph Representation 2

  36. 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).

  37. Celebrity problem uses a directed graph • At a party, a celebrity is a person who everyone knows and who knows no one (except themselves). How much time does it take to find a celebrity (if present)? • Graph: nodes are people, Edge (p1, p2) if p1 knows p2. • Is the graph directed or undirected?

  38. Definition & Application of Euler Cycle • An Euler cycleis 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.

  39. Euler Path • An Euler pathin 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 Graph G1 c d e

  40. Euler Path • a, c a b c d e

  41. Euler Path • a, c, d a b c d e

  42. Euler Path • a, c, d, e a b c d e

  43. Euler Path • a, c, d, e, b a b c d e

  44. Euler Path • a, c, d, e, b, d a b c d e

  45. Euler Path • a, c, d, e, b, d, a a b c d e

  46. Euler Path • a , c, d, e, b, d, a, b a b c d e

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

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

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

More Related