Introduction to Graphs


Published on

Introduction to Graphs

Definition: Graph
Related Definitions

Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information:

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to Graphs

  1. 1. Introduction to GraphsTecniche di Programmazione – A.A. 2012/2013
  2. 2. SummaryA.A. 2012/2013Tecniche di programmazione2 Definition: Graph Related Definitions Applications
  3. 3. Definition: GraphIntroduction to Graphs
  4. 4. Definition: GraphA.A. 2012/2013Tecniche di programmazione4 A graph is a collection of points and lines connectingsome (possibly empty) subset of them. The points of a graph are most commonly knownas graph vertices, but may also be called “nodes” orsimply “points.” The lines connecting the vertices of a graph are mostcommonly known as graph edges, but may also be called“arcs” or “lines.”
  5. 5. Big warning: Graph ≠ Graph ≠ GraphGraph (plot)(italiano: grafico)Graph (maths)(italiano: grafo)A.A. 2012/2013Tecniche di programmazione5≠Graph (chart)(italiano: grafico)
  6. 6. HistoryA.A. 2012/2013Tecniche di programmazione6 The study of graphs is known as graph theory, and wasfirst systematically investigated by D. König in the 1930s Euler’s proof about the walk across all seven bridges ofKönigsberg (1736), now known as the Königsberg bridgeproblem, is a famous precursor to graph theory. In fact, the study of various sorts of paths in graphs hasmany applications in real-world problems.
  7. 7. Königsberg Bridge ProblemA.A. 2012/2013Tecniche di programmazione7 Can the 7 bridges the ofthe city of Königsbergover the river Preger allbe traversed in a singletrip without doubling back,with the additionalrequirement that the tripends in the same place itbegan?Today: Kaliningrad, Russia
  8. 8. Königsberg Bridge ProblemA.A. 2012/2013Tecniche di programmazione8 Can the 7 bridges the ofthe city of Königsbergover the river Preger allbe traversed in a singletrip without doubling back,with the additionalrequirement that the tripends in the same place itbegan?Today: Kaliningrad, Russia
  9. 9. Types of graphs: edge cardinalityA.A. 2012/2013Tecniche di programmazione9 Simple graph: At most one edge (i.e.,either one edge or noedges) may connect anytwo vertices Multigraph: Multiple edges are allowedbetween vertices Loops: Edge between a vertex anditself Pseudograph: Multigraph with loopsloop
  10. 10. Types of graphs: edge directionA.A. 2012/2013Tecniche di programmazione10 Undirected Oriented Edges have one direction(indicated by arrow) Directed Edges may have one ortwo directions Network Oriented graph withweighted edges
  11. 11. Types of graphs: labelingA.A. 2012/2013Tecniche di programmazione11 Labels None OnVertices On Edges Groups (=colors) OfVertices no edge connects twoidentically coloredvertices Of Edges adjacent edges mustreceive different colors Of both
  12. 12. A.A. 2012/2013Tecniche di programmazione12Directed and Oriented graphs A Directed Graph (di-graph) G is a pair (V,E), where V is a (finite) set of vertices E is a (finite) set of edges, that identify a binary relationshipoverV 𝐸 ⊆ 𝑉 × 𝑉
  13. 13. A.A. 2012/2013Tecniche di programmazione13Example415 62 3
  14. 14. A.A. 2012/2013Tecniche di programmazione14Example415 62 3Loop
  15. 15. A.A. 2012/2013Tecniche di programmazione15Example415 62 3V={1,2,3,4,5,6}E={(1,2), (2,2), (2,5),(5,4), (4,5), (4,1),(2,4), (6,3)}
  16. 16. A.A. 2012/2013Tecniche di programmazione16Undirected graph Ad Undirected Graph is still represented as a coupleG=(V,E), but the set E is made of non-ordered pairs ofvertices
  17. 17. A.A. 2012/2013Tecniche di programmazione17Example415 62 3V={1,2,3,4,5,6}E={(1,2), (2,5), (5,1), (6,3)}
  18. 18. A.A. 2012/2013Tecniche di programmazione18Example415 62 3V={1,2,3,4,5,6}E={(1,2), (2,5), (5,1), (6,3)}Edge (1,5) adjacent(or incident) tovertices 1 and 5Vertex 5 is adjacentto vertices 1 and 2Vertex 4 is isolated
  19. 19. Related DefinitionsIntroduction to Graphs
  20. 20. A.A. 2012/2013Tecniche di programmazione20Degree In an undirected graph, the degree of a vertex is the number of incident edges In a directed graph The in-degree is the number of incoming edges The out-degree is the number of departing edges The degree is the sum of in-degree and out-degree A vertex with degree 0 is isolated
  21. 21. A.A. 2012/2013Tecniche di programmazione21Degree415 62 32 220 11
  22. 22. A.A. 2012/2013Tecniche di programmazione22Degree415 62 3In: 1Out: 1In: 2Out: 2In: 1 or 2Out: 2 or 3In: 2Out: 1In: 1Out: 0In: 0Out: 1
  23. 23. PathsA.A. 2012/2013Tecniche di programmazione23 A path on a graph G=(V,E) also called a trail, isa sequence {v1, v2, …, vn} such that: v1, …, vn are vertices: vi V (v1, v2), (v2, v3), ..., (vn-1,vn) are graph edges: (vi-1,vi)  E vi are distinct (for “simple” paths). The length of a path is the number of edges (n-1) If there exist a path between vA and vB we say that vB isreachable from vA
  24. 24. A.A. 2012/2013Tecniche di programmazione24Example415 62 3Path = { 1, 2, 5 }Length = 2
  25. 25. A.A. 2012/2013Tecniche di programmazione25Cycles A cycle is a path where v1 = vn A graph with no cycles is said acyclic
  26. 26. A.A. 2012/2013Tecniche di programmazione26Example415 62 3Path = { 1, 2, 5, 1 }Length = 3
  27. 27. A.A. 2012/2013Tecniche di programmazione27Reachability (Undirected) An undirected graph is connected if, for every couple ofvertices, there is a path connecting them The connected sub-graph of maximum size are calledconnected components A connected graph has exactly one connectedcomponent
  28. 28. A.A. 2012/2013Tecniche di programmazione28Connected components415 62 3The graph is notconnected.Connected components =3{ 4 } , { 1, 2, 5 }, { 3, 6 }
  29. 29. A.A. 2012/2013Tecniche di programmazione29Reachability (Directed) A directed graph is strongly connected if, for everyordered pair of vertices (v, v’), there exists at least onepath connecting v to v’
  30. 30. A.A. 2012/2013Tecniche di programmazione30Example4152The graph is stronglyconnected
  31. 31. A.A. 2012/2013Tecniche di programmazione31Example4152The graph is not stronglyconnected
  32. 32. Complete graphA.A. 2012/2013Tecniche di programmazione32 A graph is complete if, for every pair of vertices, there isan edge connecting them (they are adjacent) Symbol: Kn
  33. 33. A.A. 2012/2013Tecniche di programmazione33Complete graph: edges In a complete graph with n vertices, the number ofedges is n(n-1), if the graph is directed n(n-1)/2, if the graph is undirected If self-loops are allowed, then n2 for directed graphs n(n-1) for undirected graphs
  34. 34. DensityA.A. 2012/2013Tecniche di programmazione34 The density of a graph G=(V,E) is the ration of thenumber of edges to the total number of edges𝑑 =𝐸 𝐺𝐸 𝐾 𝑉(𝐺)
  35. 35. A.A. 2012/2013Tecniche di programmazione35Esempio4132Density = 0.5Existing: 3 edgesTotal: 6 possible edges
  36. 36. A.A. 2012/2013Tecniche di programmazione36Trees and Forests An undirected acyclic graph is called forest An undirected acyclic connected graph is called tree
  37. 37. A.A. 2012/2013Tecniche di programmazione37ExampleTree
  38. 38. A.A. 2012/2013Tecniche di programmazione38ExampleForest
  39. 39. A.A. 2012/2013Tecniche di programmazione39Example This is not a tree nor aforest(it contains a cycle)
  40. 40. Rooted treesA.A. 2012/2013Tecniche di programmazione40 In a tree, a special node may be singled out This node is called the “root” of the tree Any node of a tree can be the root
  41. 41. Tree (implicit) orderingA.A. 2012/2013Tecniche di programmazione41 The root node of a tree induces an ordering of thenodes The root is the “ancestor” of all other nodes/vertices “children” are “away from the root” “parents” are “towards the root” The root is the only node without parents All other nodes have exactly one parent The furthermost (children-of-children-of-children…)nodes are “leaves”
  42. 42. A.A. 2012/2013Tecniche di programmazione42ExampleRooted Tree
  43. 43. A.A. 2012/2013Tecniche di programmazione43ExampleRooted Tree
  44. 44. A.A. 2012/2013Tecniche di programmazione44Weighted graphs A weighted graph is a graph in which each branch (edge)is given a numerical weight. A weighted graph is therefore a special type of labeledgraph in which the labels are numbers (which are usuallytaken to be positive).
  45. 45. ApplicationsIntroduction to Graphs
  46. 46. Graph applicationsA.A. 2012/2013Tecniche di programmazione46 Graphs are everywhere Facebook friends (and posts, and ‘likes’) Football tournaments (complete subgraphs + binary tree) Google search index (V=page, E=link, w=pagerank) Web analytics (site structure, visitor paths) Car navigation (GPS) Market Matching
  47. 47. Market matchingA.A. 2012/2013Tecniche di programmazione47 H = Houses (1, 2, 3, 4) B = Buyers (a, b, c, d) V = H  B Edges: (h, b)  E if b would like to buy h Problem: can all houses be sold and allbuyers be satisfied? Variant: if the graph is weighted with apurchase offer, what is the mostconvenient solution? Variant: consider a ‘penalty’ for unsolditemsThis graph is called“bipartite”:H  B = 
  48. 48. Connecting citiesA.A. 2012/2013Tecniche di programmazione48 We have a water reservoir We need to serve many cities Directly or indirectly What is the most efficient set of inter-city waterconnections? Also for telephony,gas, electricity, …We are searching forthe “minimumspanning tree”
  49. 49. Google Analytics (Visitors Flow)A.A. 2012/2013Tecniche di programmazione49
  50. 50. Customer behaviorA.A. 2012/2013Tecniche di programmazione50User actions encodedas frequencies
  51. 51. Street navigationA.A. 2012/2013Tecniche di programmazione51We must find a“Hamiltonian cycle”TSP:The travelingsalesman problem
  52. 52. Train mapsA.A. 2012/2013Tecniche di programmazione52
  53. 53. Chemistry (Protein folding)A.A. 2012/2013Tecniche di programmazione53
  54. 54. Facebook friendsA.A. 2012/2013Tecniche di programmazione54
  55. 55. A.A. 2012/2013Tecniche di programmazione55Flow chartBEGINEND
  56. 56. ResourcesA.A. 2012/2013Tecniche di programmazione56 Maths Encyclopedia: Basic GraphTheory with Applications to Economics Application of GraphTheory in real world JGraphT Library:
  57. 57. Licenza d’usoA.A. 2012/2013Tecniche di programmazione57 Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CCBY-NC-SA)” Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,rappresentare, eseguire e recitare questopera di modificare questopera Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dellopera agli autorioriginali e in modo tale da non suggerire che essi avallino te o il modo incui tu usi lopera. Non commerciale — Non puoi usare questopera per finicommerciali. Condividi allo stesso modo — Se alteri o trasformi questopera, o sela usi per crearne unaltra, puoi distribuire lopera risultante solo con unalicenza identica o equivalente a questa.