slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Graphs PowerPoint Presentation
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 61

Graphs - PowerPoint PPT Presentation

  • Uploaded on

Graphs. 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms. What is a graph?. Definition

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Graphs' - adolph

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript


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

What is a graph?

  • Definition
  • A graphG is a finite set V of verticesand a finite set E of edgesconnecting 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 are
  • directed (bottom fig.).


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


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


  • The end-verticesof 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 incidentto a vertex if it connects that vertex to another vertex.
  • The degreeof a vertex is the number of edges that are incident to that vertex.


  • 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


b e c

a c d a

  • 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.


  • 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


  • Tree – connex graph without cycle
  • Forest - collection of trees


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 notcomplete then m < n(n-1)/2

n = 5

m= (5 * 4)/2 = 10


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


  • In case of a tree m = n - 1
  • if m < n - 1, G is not connex

n = 5

m = 4

n = 5

m = 3


Spanning tree

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

G Spanning tree ofG



  • 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?


  • 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!

More definitions

  • Oriented graph: each edge go only in one direction
  • Acyclic oriented graph

Without cycle

With cycle



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


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 }


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 to
  • vertex j.

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.

Exploration algorithms

  • They explore the vertices that are reachable starting from a source vertex.
  • Depth-First Search
  • Breadth-First Search (level-order search)

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.

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.

Depth-First Search

  • 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.

Algorithme DFS(u);

Input: a vertex u of G

Output: a graph with all vertices labeled ‘visited’

for each edge e incident to u do

let v be the other extremity of e

if vertex vis not ‘visited’ then

mark v ‘visited’

recursively call DFS(v)











Definition of a weighted graph

  • A graph, in which each edge has an associated numerical value, is called
  • a 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.

Definition of the MST (Minimum Spanning Tree)

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


The weight of the MST:

W(MST) = 8 + 2 + 4 + 7 + 4 + 2 + 9 + 1 = 37 is minimal.


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


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:

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

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

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

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.

Prim’s algorithm for finding a MST

  • Minimum-Spanning-Tree-by-Prim(G, weight-function, source)
    • for each vertex uin graph G
      • set key ofuto ∞
      • set parent ofutonil
    • set key of source vertex tozero
    • enqueue to minimum-heap Q all vertices in graph G.
    • whileQ is not empty
      • extract vertex u from Q// u is the vertex with the lowest key that is in Q
      • for each adjacent vertex vofudo
        • if (v is still in Q) and (weight-function(u, v) < key of v) then
          • setuto be parent ofv// in minimum-spanning-tree
          • update v's key to equal weight-function(u, v)
  • Complexity: O(nlogn + mlogn) using a heap

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. . .

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.


  • 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.

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 final

shortest path weights is determined.


Djikstra’s formal algorithm

  • Djikstra(G,s)
    • input: G = (V,E), s is the source vertex
    • output: Shortest paths
    • for each v V
      • v.distance = ;
      • v.parent = null;
      • s.distance = 0;
    • Q = V; //Q is a priority queue
    • Cloud = ; //cloud is empty
    • while(!Q.isEmpty())
      • u = Q.extract minimum()
      • Cloud = Cloud  {u};
      • for each v not in cloud adjacent to u
        • relax(u,v,Q);

Complexity (using a heap):

O(mlogn + nlogn)

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