Introduction to Graphs -...

31
4/15/2014 1 Introduction to Graphs Tecniche di Programmazione Summary A.A. 2012/2013 Tecniche di programmazione 2 Definition: Graph Related Definitions Applications

Transcript of Introduction to Graphs -...

Page 1: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

1

Introduction to Graphs

Tecniche di Programmazione

Summary

A.A. 2012/2013 Tecniche di programmazione 2

Definition: Graph

Related Definitions

Applications

Page 2: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

2

Definition: Graph

Introduction to Graphs

Definition: Graph

A.A. 2012/2013 Tecniche di programmazione 4

Page 3: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

3

Definition: Graph

A.A. 2012/2013 Tecniche di programmazione 5

A graph is a collection of points and lines

connecting some (possibly empty) subset of them.

The points of a graph are most commonly known

as graph vertices, but may also be called “nodes”

or simply “points.”

The lines connecting the vertices of a graph are

most commonly known as graph edges, but may

also be called “arcs” or “lines.”

Definition: Graph

A.A. 2012/2013 Tecniche di programmazione 6

A graph is an ordered pair G = (V, E)

a set V of “vertices”

a set E of “edges”, which are 2-element subsets of V

(i.e., E is a relation in V2).

Page 4: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

4

What's in a name?

A.A. 2012/2013 Tecniche di programmazione 7

http://spikedmath.com/382.html

Big warning: Graph ≠ Graph ≠ Graph

Graph (plot)

(italiano: grafico)

Graph (maths)

(italiano: grafo)

A.A. 2012/2013 Tecniche di programmazione 8

Graph (chart)

(italiano: grafico)

Page 5: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

5

History

A.A. 2012/2013 Tecniche di programmazione 9

The study of graphs is known as graph theory, and

was first systematically investigated by D. König in

the 1930s

Euler’s proof about the walk across all seven bridges

of Königsberg (1736), now known as the Königsberg

bridge problem, is a famous precursor to graph

theory.

In fact, the study of various sorts of paths in graphs

has many applications in real-world problems.

Königsberg Bridge Problem

A.A. 2012/2013 Tecniche di programmazione 10

Can the 7 bridges the of

the city of Königsberg

over the river Preger all

be traversed in a single

trip without doubling

back, with the additional

requirement that the trip

ends in the same place

it began?

Today: Kaliningrad, Russia

Page 6: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

6

Königsberg Bridge Problem

A.A. 2012/2013 Tecniche di programmazione 11

Can the 7 bridges the of

the city of Königsberg

over the river Preger all

be traversed in a single

trip without doubling

back, with the additional

requirement that the trip

ends in the same place

it began?

Today: Kaliningrad, Russia

Unless…

A.A. 2012/2013 Tecniche di programmazione 12

http://spikedmath.com/541.html

Page 7: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

7

Types of graphs: edge cardinality

A.A. 2012/2013 Tecniche di programmazione 13

Simple graph: At most one edge (i.e.,

either one edge or no edges) may connect any two vertices

Multigraph:

Multiple edges are allowed between vertices

Loops: Edge between a vertex

and itself

Pseudograph: Multigraph with loops

loop

Types of graphs: edge direction

A.A. 2012/2013 Tecniche di programmazione 14

Undirected

Oriented

Edges have one

direction (indicated by

arrow)

Directed

Edges may have one or

two directions

Network

Oriented graph with

weighted edges

Page 8: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

8

Types of graphs: labeling/coloring

A.A. 2012/2013 Tecniche di programmazione 15

Labels None

On Vertices

On Edges

Groups (=colors) Of Vertices

no edge connects two identically colored vertices

Of Edges

adjacent edges must receive different colors

Of both

A.A. 2012/2013 Tecniche di programmazione 16

Directed 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

relationship over V

Page 9: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

9

A.A. 2012/2013 Tecniche di programmazione 17

Example

4

1

5 6

2 3

A.A. 2012/2013 Tecniche di programmazione 18

Example

4

1

5 6

2 3

Loop

Page 10: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

10

A.A. 2012/2013 Tecniche di programmazione 19

Example

4

1

5 6

2 3

V={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/2013 Tecniche di programmazione 20

Undirected graph

Ad Undirected Graph is still represented as a

couple G=(V,E), but the set E is made of non-

ordered pairs of vertices

Page 11: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

11

A.A. 2012/2013 Tecniche di programmazione 21

Example

4

1

5 6

2 3

V={1,2,3,4,5,6}

E={(1,2), (2,5), (5,1),

(6,3)}

A.A. 2012/2013 Tecniche di programmazione 22

Example

4

1

5 6

2 3

V={1,2,3,4,5,6}

E={(1,2), (2,5), (5,1),

(6,3)}

Edge (1,5) adjacent

(or incident) to

vertices 1 and 5

Vertex 5 is adjacent

to vertices 1 and 2

Vertex 4 is isolated

Page 12: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

12

Related Definitions

Introduction to Graphs

A.A. 2012/2013 Tecniche di programmazione 24

Degree

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

Page 13: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

13

A.A. 2012/2013 Tecniche di programmazione 25

Degree

4

1

5 6

2 3

2 2

2 0 1

1

A.A. 2012/2013 Tecniche di programmazione 26

Degree

4

1

5 6

2 3

In: 1

Out: 1

In: 2

Out: 2

In: 1 or 2

Out: 2 or 3

In: 2

Out: 1

In: 1

Out: 0

In: 0

Out: 1

Page 14: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

14

Paths

A.A. 2012/2013 Tecniche di programmazione 27

A path on a graph G=(V,E) also called a trail, is

a 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

is reachable from vA

A.A. 2012/2013 Tecniche di programmazione 28

Example

4

1

5 6

2 3

Path = { 1, 2, 5 }

Length = 2

Page 15: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

15

A.A. 2012/2013 Tecniche di programmazione 29

Cycles

A cycle is a path where v1 = vn

A graph with no cycles is said acyclic

A.A. 2012/2013 Tecniche di programmazione 30

Example

4

1

5 6

2 3

Path = { 1, 2, 5, 1 }

Length = 3

Page 16: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

16

A.A. 2012/2013 Tecniche di programmazione 31

Reachability (Undirected)

An undirected graph is connected if, for every

couple of vertices, there is a path connecting them

The connected sub-graph of maximum size are

called connected components

A connected graph has exactly one connected

component

A.A. 2012/2013 Tecniche di programmazione 32

Connected components

4

1

5 6

2 3

The graph is not

connected.

Connected components

= 3

{ 4 } , { 1, 2, 5 }, { 3, 6 }

Page 17: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

17

A.A. 2012/2013 Tecniche di programmazione 33

Reachability (Directed)

A directed graph is strongly connected if, for every

ordered pair of vertices (v, v’), there exists at least

one path connecting v to v’

A.A. 2012/2013 Tecniche di programmazione 34

Example

4

1

5

2

The graph is strongly

connected

Page 18: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

18

A.A. 2012/2013 Tecniche di programmazione 35

Example

4

1

5

2

The graph is not

strongly connected

Eulerian cycle (again)

A.A. 2012/2013 Tecniche di programmazione 36

In 1736, Euler proved that a necessary condition

for the existence of Eulerian circuits is that all

vertices in the connected graph have an even

degree, and stated (without proof) that connected

graphs with all vertices of even degree have an

Eulerian circuit.

The first complete proof of this latter

claim was published posthumously

in 1873 by Carl Hierholzer.

Page 19: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

19

Complete graph

A.A. 2012/2013 Tecniche di programmazione 37

A graph is complete if, for every pair of vertices,

there is an edge connecting them (they are adjacent)

Symbol: Kn

A.A. 2012/2013 Tecniche di programmazione 38

Complete graph: edges

In a complete graph with n vertices, the number of

edges 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

Page 20: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

20

Density

A.A. 2012/2013 Tecniche di programmazione 39

The density of a graph G=(V,E) is the ration of the

number of edges to the total number of edges

A.A. 2012/2013 Tecniche di programmazione 40

Example

4

1

3

2

Density = 0.5

Existing: 3 edges

Total: 6 possible edges

Page 21: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

21

A.A. 2012/2013 Tecniche di programmazione 41

Trees and Forests

An undirected acyclic graph is called forest

An undirected acyclic connected graph is called tree

A.A. 2012/2013 Tecniche di programmazione 42

Example Tree

Page 22: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

22

A.A. 2012/2013 Tecniche di programmazione 43

Example Forest

A.A. 2012/2013 Tecniche di programmazione 44

Example This is not a tree nor a

forest

(it contains a cycle)

Page 23: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

23

Rooted trees

A.A. 2012/2013 Tecniche di programmazione 45

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) ordering

A.A. 2012/2013 Tecniche di programmazione 46

The root node of a tree induces an ordering of the

nodes

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”

Page 24: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

24

A.A. 2012/2013 Tecniche di programmazione 47

Example Rooted Tree

A.A. 2012/2013 Tecniche di programmazione 48

Example Rooted Tree

Page 25: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

25

A.A. 2012/2013 Tecniche di programmazione 49

Weighted 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 labeled graph in which the labels are numbers

(which are usually taken to be positive).

Applications

Introduction to Graphs

Page 26: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

26

Graph applications

A.A. 2012/2013 Tecniche di programmazione 51

Graphs are everywhere

Facebook friends (and posts, and ‘likes’)

Football tournaments (complete subgraphs)

Google search index (V=page, E=link, w=pagerank)

Web analytics (site structure, visitor paths)

Car navigation (GPS)

Market Matching

Market matching

A.A. 2012/2013 Tecniche di programmazione 52

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 all buyers be satisfied?

Variant: if the graph is weighted with a purchase offer, what is the most convenient solution?

Variant: consider a ‘penalty’ for unsold items

This graph is called

“bipartite”:

H B =

Page 27: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

27

Connecting cities

A.A. 2012/2013 Tecniche di programmazione 53

We have a water reservoir

We need to serve many cities

Directly or indirectly

What is the most efficient set of inter-city water

connections?

Also for telephony,

gas, electricity, …

We are searching

for the “minimum

spanning tree”

Google Analytics (Visitors Flow)

A.A. 2012/2013 Tecniche di programmazione 54

Page 28: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

28

Customer behavior

A.A. 2012/2013 Tecniche di programmazione 55

User actions

encoded as

frequencies

Street navigation

A.A. 2012/2013 Tecniche di programmazione 56

We must find a

“Hamiltonian cycle”

TSP: The traveling

salesman problem

Page 29: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

29

Train maps

A.A. 2012/2013 Tecniche di programmazione 57

Chemistry (Protein folding)

A.A. 2012/2013 Tecniche di programmazione 58

Page 30: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

30

Facebook friends

A.A. 2012/2013 Tecniche di programmazione 59

A.A. 2012/2013 Tecniche di programmazione 60

Flow chart

BEGIN

END

Page 31: Introduction to Graphs - polito.itelite.polito.it/files/courses/03FYZ/2014/slide/07-intro-graphs.pdf · Introduction to Graphs 24 Tecniche di programmazione A.A. 2012/2013 Degree

4/15/2014

31

Resources

A.A. 2012/2013 Tecniche di programmazione 61

Maths Encyclopedia: http://mathworld.wolfram.com/

Basic Graph Theory with Applications to Economics

http://www.isid.ac.in/~dmishra/mpdoc/lecgraph.pdf

Application of Graph Theory in real world

http://prezi.com/tseh1wvpves-/application-of-graph-

theory-in-real-world/

JGraphT Library: http://jgrapht.org/

Licenza d’uso

A.A. 2012/2013 Tecniche di programmazione 62

Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”

Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in

pubblico, rappresentare, eseguire e recitare quest'opera

di modificare quest'opera

Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori

originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.

Non commerciale — Non puoi usare quest'opera per fini commerciali.

Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.

http://creativecommons.org/licenses/by-nc-sa/3.0/