230 likes | 367 Views
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. What we mean by “graphs”.
E N D
CS1022Computer 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 CS1022
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 1 5 6 2 3 4 CS1022
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: • Find a route starting and ending at the same place • Traversing all bridges exactly once Leonhard Euler CS1022
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 CS1022
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 CS1022
Terminology (1) C c • 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) g d e A D b f a B CS1022
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 CS1022
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 tov. • 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 CS1022
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 • Takes as input a graph • Checks the property • It’s a simpler problem than computing all paths/routes, starting from all nodes and back • Outputs • Yes, it is an Eulerian graph • No, it is not an Eulerian graph CS1022
Simple graphs (1) • A simple graphG 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) CS1022
Simple graphs (2) • In simple graphs, vertices u and v are adjacent if they are connected by an edgee • 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 CS1022
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 CS1022
Example Suppose G (V, E) • V a, b, c, d, e • E ab, ae, bc, bd, ce, de Draw simple graph Show adjacency matrix b c a e d CS1022
Subgraph • A subgraphof G (V, E) is any graph G (V, E) such that VVand EE • That is, a subgraph has a subset of vertices and edges • N.B.: if removing vertices, then edges may need to go CS1022
Paths • A path of length kin 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 v0v1 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 b c a e d CS1022
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 b c a e d CS1022
Acyclic and connected graphs • A graph for which we cannot find cyclesis called an acyclic graph • A graph is connected if there is a path between every pair of vertices • Example of graph acyclic and connected: b c a e d CS1022
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 CS1022
Connectivity algorithm (1) • The algorithm below computes the value c(G) CS1022
Connectivity algorithm (2) • Trace algorithm with input 2 3 4 1 5 6 7 8 CS1022
Connectivity algorithm (3) • Therefore c(G) 3 • The 3 connected components are: • N.B.: algorithm does not compute these! 2 4 3 1 7 5 6 8 CS1022
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 CS1022