Introduction to Graphs
Upcoming SlideShare
Loading in...5

Introduction to Graphs



Introduction to Graphs ...

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:



Total Views
Views on SlideShare
Embed Views



1 Embed 1 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Introduction to Graphs Introduction to Graphs Presentation Transcript

  • Introduction to GraphsTecniche di Programmazione – A.A. 2012/2013
  • SummaryA.A. 2012/2013Tecniche di programmazione2 Definition: Graph Related Definitions Applications
  • Definition: GraphIntroduction to Graphs
  • 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.”
  • Big warning: Graph ≠ Graph ≠ GraphGraph (plot)(italiano: grafico)Graph (maths)(italiano: grafo)A.A. 2012/2013Tecniche di programmazione5≠Graph (chart)(italiano: grafico)
  • 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.
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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 𝐸 ⊆ 𝑉 × 𝑉
  • A.A. 2012/2013Tecniche di programmazione13Example415 62 3
  • A.A. 2012/2013Tecniche di programmazione14Example415 62 3Loop
  • 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)}
  • 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
  • A.A. 2012/2013Tecniche di programmazione17Example415 62 3V={1,2,3,4,5,6}E={(1,2), (2,5), (5,1), (6,3)}
  • 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
  • Related DefinitionsIntroduction to Graphs
  • 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
  • A.A. 2012/2013Tecniche di programmazione21Degree415 62 32 220 11
  • A.A. 2012/2013Tecniche di programmazione22Degree415 62 3In: 1Out: 1In: 2Out: 2In: 1 or 2Out: 2 or 3In: 2Out: 1In: 1Out: 0In: 0Out: 1
  • 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
  • A.A. 2012/2013Tecniche di programmazione24Example415 62 3Path = { 1, 2, 5 }Length = 2
  • A.A. 2012/2013Tecniche di programmazione25Cycles A cycle is a path where v1 = vn A graph with no cycles is said acyclic
  • A.A. 2012/2013Tecniche di programmazione26Example415 62 3Path = { 1, 2, 5, 1 }Length = 3
  • 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
  • A.A. 2012/2013Tecniche di programmazione28Connected components415 62 3The graph is notconnected.Connected components =3{ 4 } , { 1, 2, 5 }, { 3, 6 }
  • 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’
  • A.A. 2012/2013Tecniche di programmazione30Example4152The graph is stronglyconnected
  • A.A. 2012/2013Tecniche di programmazione31Example4152The graph is not stronglyconnected
  • 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
  • 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
  • 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𝑑 =𝐸 𝐺𝐸 𝐾 𝑉(𝐺)
  • A.A. 2012/2013Tecniche di programmazione35Esempio4132Density = 0.5Existing: 3 edgesTotal: 6 possible edges
  • A.A. 2012/2013Tecniche di programmazione36Trees and Forests An undirected acyclic graph is called forest An undirected acyclic connected graph is called tree
  • A.A. 2012/2013Tecniche di programmazione37ExampleTree
  • A.A. 2012/2013Tecniche di programmazione38ExampleForest
  • A.A. 2012/2013Tecniche di programmazione39Example This is not a tree nor aforest(it contains a cycle)
  • 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
  • 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”
  • A.A. 2012/2013Tecniche di programmazione42ExampleRooted Tree
  • A.A. 2012/2013Tecniche di programmazione43ExampleRooted Tree
  • 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).
  • ApplicationsIntroduction to Graphs
  • 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
  • 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 = 
  • 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”
  • Google Analytics (Visitors Flow)A.A. 2012/2013Tecniche di programmazione49
  • Customer behaviorA.A. 2012/2013Tecniche di programmazione50User actions encodedas frequencies
  • Street navigationA.A. 2012/2013Tecniche di programmazione51We must find a“Hamiltonian cycle”TSP:The travelingsalesman problem
  • Train mapsA.A. 2012/2013Tecniche di programmazione52
  • Chemistry (Protein folding)A.A. 2012/2013Tecniche di programmazione53
  • Facebook friendsA.A. 2012/2013Tecniche di programmazione54
  • A.A. 2012/2013Tecniche di programmazione55Flow chartBEGINEND
  • ResourcesA.A. 2012/2013Tecniche di programmazione56 Maths Encyclopedia: Basic GraphTheory with Applications to Economics Application of GraphTheory in real world JGraphT Library:
  • 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.