Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)

Similar presentations


Presentation on theme: "CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)"— Presentation transcript:

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

2 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 2 CS1022

3 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 3 CS

4 Origins of graphs (1) Leonhard Euler (18 th 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 place 2.Traversing all bridges exactly once 4 CS1022 Leonhard Euler

5 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 5 CS1022

6 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  6 CS1022

7 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) 7 CS1022 A B C D a b c d e f g

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

9 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 9 CS1022

10 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 graph 2.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 10 CS1022

11 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) 11 CS1022

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

13 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 13 CS1022 v1v1 v2v2 v3v3 v4v4 v1v1 FTFT v2v2 TFTT v3v3 FTFF v4v4 TTFF

14 Example Suppose G  (V, E) V   a, b, c, d, e  E   ab, ae, bc, bd, ce, de  Draw simple graphShow adjacency matrix 14 CS1022 abcde aF bF cF dF eF a b e c d abcde aFT bTF cF dF eF abcde aFTT bTF cF dF eTF abcde aFTT bTFT cTF dF eTF abcde aFTT bTFTT cTF dTF eTF abcde aFTT bTFTT cTFT dTF eTTF abcde aFTT bTFTT cTFT dTFT eTTTF abcde aFTFFT bTFTTF cFTFFT dFTFFT eTFTTF abcde aFTFFT bTFTTF cFTFFT dFTFFT eTFTTF

15 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 15 CS1022

16 Paths A path of length k in a graph is a sequence of distinct vertices v 0, v 1, , v k such that v i–1 v i is an edge, 1  i  k We denote this path as v 0 v 1  v k 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 16 CS1022 a b e c d

17 Cycles A cycle in a graph is a sequence of vertices v 0, v 1, , v k such that – v i–1 v i is an edge, 1  i  k (that is, it is a path) – v 0  v k (first and last vertices are the same) – v i–1 v i, v i–1  v i, 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 17 CS1022 a b e c d

18 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: 18 CS1022 a b e c d

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

20 Connectivity algorithm (1) The algorithm below computes the value c(G) 20 CS1022 input graph G consisting of pair (V, E) begin V:= V; c := 0; while V   do begin 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; end output c end

21 Connectivity algorithm (2) Trace algorithm with input 21 CS1022 input graph G consisting of pair (V, E) begin V:= V; c := 0; while V   do begin 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; end output c end V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1  2, 4, 5, 7  1 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1  2, 4, 5, 7  1 Choose y = 2 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1  2, 4, 5, 7  1 Choose y = 2 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1  2, 4, 5, 7  1 Choose y = 2{7}2 V c Initial values  1, 2, 3, 4, 5, 6, 7, 8  0 Choose y = 1  2, 4, 5, 7  1 Choose y = 2{7}2 Choose y = 7  3

22 Connectivity algorithm (3) Therefore c(G)  3 The 3 connected components are: N.B.: algorithm does not compute these! 22 CS

23 Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 7) Wikipedia’s entry on graph theory Wikibooks entry on graph theory 23 CS1022


Download ppt "CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)"

Similar presentations


Ads by Google