Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency...

61
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation • Adjacency list • Adjacency matrix 5 Exploration algorithms 6 Other algorithms

Transcript of Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency...

Page 1: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Graphs

1 Definition

2 Terminology

3 Properties

4 Internal representation

• Adjacency list

• Adjacency matrix

5 Exploration algorithms

6 Other algorithms

Page 2: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Definition

A graph G is a finite set V of vertices and a finite set E of edges connecting pairs of vertices:

G = (V,E)

Directed vs. undirected

• G is undirected if its edges are undirected (top fig.),

• G is directed if its edges aredirected (bottom fig.).

What is a graph?

Page 3: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Applications

• Network representation: traffic, aerial routing, internet…• Automata: languages, discrete state systems• Dynamic system modeling• Probabilistic model: Bayesian network, neural network…

Algorithms

• Shortest path• Optimal flow• Optimal tour: traveling salesman• Clustering: k-neighboring• Complexity of a network

Page 4: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• The end-vertices of an edge are the vertices connected by that edge.

• Adjacent vertices: directly linked by an edge

• Adjacent edges: share a common end-vertex

• An edge is incident to a vertex if it connects that vertex to another vertex.

• The degree of a vertex is the number of edges that are incident to that vertex.

Terminology

Page 5: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Properties

Let G be a graph with n vertices and m edges.

Property 1

Proof: each edge is counted twice.

Property 2

If G is undirected with no self-loops and no multiple edges:

m ≤ n(n − 1)/2

Proof: the maximum number of edges is obtained when each vertex is connected to all the other vertices of the graph. We have,

(n − 1) + (n − 2) + (n − 3) + . . . + 2 + 1 = n(n − 1)/2

Page 6: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• Path: sequence of vertices v1 ,v2 ,. . .vk such that all consecutive vertices are adjacent.

Simple path : no repeated vertex

Cycle: simple path, excepted that the last vertex is the same as the first one.

Terminology

b e c

a c d a

Page 7: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• Connex graph: each pair of vertices is linked by a path

• Sub-graph: sub-set of vertices and edges forming a graph

• Connex component: sub-graph connex

example: le graph bellow has 3 connex components

Terminology

Page 8: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• Tree – connex graph without cycle

• Forest - collection of trees

Terminology

Page 9: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Connectivity

Let n = # vertices

m = # edges• Complete graph (clique) – each pair of vertices are

adjacent

• Each of the n vertices are incident to n-1 edges, but each edge is summed two time! Therefore, m = n(n-1)/2.

• So iff a graph is not complete then m < n(n-1)/2

2/)1()1(2

1)deg(

2

1

Vv Vv

nnnvm

n 5m(5

Page 10: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• Clique: A subgraph in which each pair of vertices are adjacent: a complete subgraph.

• Search for the maximum clique: a naïve algorithm.

• Examine each set of k vertices to determine if it is a clique.

• But the number of possible cliques of size k in a graph of size V

• Lot of research on heuristic algorithms to find good non-exact solutions

Page 11: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• In case of a tree m = n - 1

• if m < n - 1, G is not connex

Connectivity

n 5m 4

n 5m 3

Page 12: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

A spanning tree of G is a sub-graph which is a tree and which contains all vertices of G

G Spanning tree of G

Spanning tree

Page 13: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Curiosity

Euler and the bridges of Koenigsberg: the first problem of graph theory?

• Is it possible to make a walk crossing each bridge one and only one time and to come back to the starting point?

Page 14: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• The graph model

• Eulerian circuit: path which use each edge exactly once and come back to the initial vertex. • Euler’s theorem: a connected graph has an Eulerian circuit iff it has no vertex of odd degree

No, it is not possible!

Curiosity

Page 15: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

More definitions

• Oriented graph: each edge go only in one direction

• Acyclic oriented graph

Without cycle With cycle

Page 16: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Accessibility

A tree rooted in v contains all accessible vertices from v using oriented path

strongly connex

each vertex is accessible from each other using an oriented path

Page 17: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Strongly connex component

Transitive closure

It is the graph G* obtained from the graph G after applying the following rule:

If there exists an oriented path from a to b in G then add an oriented edge from a to b in G*.

{ a , c , g }

{ f , d , e , b }

Page 18: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Graph representation

Adjacency list

Definition• The adjacency list of a graph with n vertices is an array of n lists of vertices.

• The list i contains vertex j if there is an edge from vertex i tovertex j.

Page 19: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Example 2 in an oriented graph

Page 20: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Adjacency matrix

Definition

• Let adjacency matrix of a graph with n vertices is a n × n matrix A where:

Remark

The adjacency matrix of an undirected graph must be symmetric.

Page 21: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Example 1

Page 22: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Example 2

Page 23: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• They explore the vertices that are reachable starting from a source vertex.

• Depth-First Search

• Breadth-First Search (level-order search)

Exploration algorithms

Page 24: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Breath-First Search

• Algorithm that explores the vertices that are reachable starting from a source vertex s and constructs a breadth-first search tree (spanning tree).

• Compute a distance from each vertices to the source vertex.

Color terminology

• WHITE vertices are unexplored.

• BLACK vertices are fully explored vertices.

• GRAY vertices are being explored: these vertices define the ”frontier” between explored and unexplored vertices.

Page 25: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

BSF algorithm

Page 26: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Example

Page 27: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

The resulting BSF spanning tree

Page 28: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Analysis of BFS

• Let n be the number of vertices and m the number of edges in a graph.

• Each vertex is enqueued once in the queue: to enqueue all vertices it takes O(n).

• Each edge is visited at most once: visiting all edges takes O(m).

• Complexity of BFS

As a result, BFS takes O(n + m) time.

Page 29: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

• Start from a vertex s.

• Set s as the current vertex u. Mark u as ‘visited’.

• Select arbitrarily one adjacent vertex v of u.

• If v is ‘visited’ go back to u

• Else mark v ‘visited’. V become the current vertex. Repeat the previous steps

• When all vertices adjacent to the current vertex are ‘visited’ backtrack to a previous ‘visited’ vertex. Repeat the previous steps.

• When backtrack leads to vertex s and if all the adjacent vertices of s are ‘visited’, the algorithm stop.

Depth-First Search

Page 30: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Algorithme DFS(u);Input: a vertex u of GOutput: a graph with all vertices labeled ‘visited’

for each edge e incident to u dolet v be the other extremity of eif vertex v is not ‘visited’ then

mark v ‘visited’recursively call DFS(v)

Page 31: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Example

1) 2)

3) 4)

Page 32: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

5) 6)

Page 33: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Definition of a weighted graph

• A graph, in which each edge has an associated numerical value, is calleda weighted graph.

• The numerical value associated to an edge is the weight of the edge.

• The weight of an edge can represent a distance, a cost. . . etc.

Applications

• Weighted graphs find their application in various problems such as communication or transportation networks.

Page 34: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Definition of the MST (Minimum Spanning Tree)

The MST is a spanning tree of a connex, weighted and undirected graph with minimum total weight.

Example

The weight of the MST:W(MST) = 8 + 2 + 4 + 7 + 4 + 2 + 9 + 1 = 37 is minimal.

Page 35: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Formal definition of a MST

Given a connex, weighted and undirected graph G = (V,E), find an acyclic subset T E connecting all vertices in V such that:

weight(u, v) is the weight the edge (u, v).

Page 36: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Safe edges

Definition

• Let A be a subset of edges of a MST of a graph G.

• An edge (u, v) of G is safe for A if A {(u, v)} is also a subset of a MST.

• We can deduce from the above definition that finding a MST can be done by greedily grow a set of safe edges:

Page 37: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

More definitions...

The cut of a graph

• A cut of a graph G = (V,E) is a partition of the vertices of the graph into 2 sets: S and V − S.

• The cut is denoted: (S,V − S).

Page 38: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

An edge crossing the cut

• An edge crosses the cut (S,V − S) if one of its end-vertices is in S and the other one in V − S.

• The edges (b, c), (c, d), (d, f ), (a, h), (e, f) and (b, h) cross the cut (S,V-S).

Page 39: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

A cut respects a set...

• A cut respects a set A of edges if no edge in A crosses the cut.

Light edges

An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

Characterization of a safe edge

Theorem

• Let G = (V,E) and A E included in some MST of G.

• Let (S,V − S) be a cut of G that respects A.

• Let e = (u, v) be a light edge crossing (S,V − S).

• Then, edge e is safe for A, which mean that e is in the MST of G

Page 40: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Proof by contradiction

• Suppose you have a MST T not containing e; then we want to show that T is not the MST.

• Let e=(u,v), with u in S and v in V - S.

• Then because T is a spanning tree it contains a unique path from u to v, which together with e forms a cycle in G.

• This path has to include another edge f connecting S to V - S.

• T+e-f is another spanning tree (it has the same number of edges, and remains connected since you can replace any path containing f by one going the other way around the cycle).

• It has smaller weight than T since e has smaller weight than f.

• So T was not minimum, which is what we wanted to prove.

Page 41: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Prim’s algorithm for finding a MST

Minimum-Spanning-Tree-by-Prim(G, weight-function, source)

for each vertex u in graph G

set key of u to ∞

set parent of u to nil

set key of source vertex to zero

enqueue to minimum-heap Q all vertices in graph G.

while Q is not empty

extract vertex u from Q // u is the vertex with the lowest key that is in Q

for each adjacent vertex v of u do

if (v is still in Q) and (weight-function(u, v) < key of v) then

set u to be parent of v // in minimum-spanning-tree

update v's key to equal weight-function(u, v)

Complexity: O(nlogn + mlogn) using a heap

Page 42: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Prim’s algorithm on an example...

Page 43: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 44: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 45: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 46: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 47: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 48: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 49: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 50: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 51: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 52: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Shortest path

Weight (or length) of a path• In a weighted graph, the weight (or length) of a path is the sum of the weights of its edges.

Shortest path• Given a weighted graph and two vertices u and v, find the path of minimum weight between u and v.

Property• A subpath of a shortest path is itself a shortest path. (Proof: by contradiction.)

Applications• Networks,• Driving directions,• Flights. . .

Page 53: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Djikstra’s algorithm for shortest path

Definition

• Djikstra’s algorithm for shortest path incrementally constructs a set of vertices (or cloud) to which the shortest path is known.

• At each iteration, the algorithm adds to the cloud a vertex v (not in the cloud) whose the distance to the source is the shortest of the remaining vertices that are not in the cloud.

Assumption

• Djikstra’s algorithm assumes that the weights are non-negative.

Page 54: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Relaxation

• Let v.distance be the shortest ”known” path between vertex v (not in the cloud) and the source vertex.

• When u is added to the cloud, we discover a new path (that contains u) from the source to v. In this case, v.distance may (or may not) change:

v.distance = min(v.distance, u.distance + weight(u, v))

Note: The values in the vertices represent the distance from the vertex to the source.

Page 55: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Dijkstra’s algorithm on an example...

Initialize all distances to infinity except for the source vertex which distance is zero (from itself).

Goal: incrementally construct a cloud of vertices whose finalshortest path weights is determined.

Page 56: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 57: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 58: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 59: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 60: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Page 61: Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.

Djikstra’s formal algorithm

Djikstra(G,s)input: G = (V,E), s is the source vertexoutput: Shortest pathsfor each v V

v.distance = ;v.parent = null;s.distance = 0;

Q = V; //Q is a priority queueCloud = ; //cloud is emptywhile(!Q.isEmpty())

u = Q.extract minimum()Cloud = Cloud {u};for each v not in cloud adjacent to u

relax(u,v,Q);Relax(u,v,Q)

if (v.distance > u.distance + weight(u, v))v.distance = u.distance + weight(u, v);v.parent = u;update Q

Complexity (using a heap):

O(mlogn + nlogn)