Graphs - PowerPoint PPT Presentation

About This Presentation
Title:

Graphs

Description:

Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 62
Provided by: rgr115
Category:

less

Transcript and Presenter's Notes

Title: Graphs


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

2
What is a graph?
  • 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 are
  • directed (bottom fig.).

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

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

5
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
6
Terminology
  • 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.

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

8
Terminology
  • Tree connex graph without cycle
  • Forest - collection of trees

9
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 lt n(n-1)/2

n 5 m (5 4)/2 10
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

11
Connectivity
  • In case of a tree m n - 1
  • if m lt n - 1, G is not connex

n 5 m 4
n 5 m 3
12
Spanning tree
  • 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
13
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?

14
Curiosity
  • The graph model
  • Eulerian circuit path which use each edge
    exactly once and come back to the initial vertex.
  • Eulers theorem a connected graph has an
    Eulerian circuit iff it has no vertex of odd
    degree
  • ? No, it is not possible!

15
  • More definitions
  • Oriented graph each edge go only in one
    direction
  • Acyclic oriented graph

Without cycle
With cycle
16
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
17
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
18
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.

19
Example 2 in an oriented graph
20
  • 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.

21
Example 1
22
Example 2
23
Exploration algorithms
  • They explore the vertices that are reachable
    starting from a source vertex.
  • Depth-First Search
  • Breadth-First Search (level-order search)

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

25
BSF algorithm
26
Example
27
The resulting BSF spanning tree
28
  • 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.

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

30
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 v is not visited then mark v
visited recursively call DFS(v)
31
Example
2)
1)
3)
4)
32
5)
6)
33
  • 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.

34
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.
35
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).
36
  • 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

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

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

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

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

41
Prims 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 8 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) lt 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
42
Prims algorithm on an example...
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
(No Transcript)
49
(No Transcript)
50
(No Transcript)
51
(No Transcript)
52
  • 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. . .

53
  • Djikstras algorithm for shortest path
  • Definition
  • Djikstras 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
  • Djikstras algorithm assumes that the weights
    are non-negative.

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

55
Dijkstras 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.
56
(No Transcript)
57
(No Transcript)
58
(No Transcript)
59
(No Transcript)
60
(No Transcript)
61
Djikstras 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 gt u.distance
weight(u, v)) v.distance u.distance weight(u,
v) v.parent u update Q
Write a Comment
User Comments (0)
About PowerShow.com