1 / 61

Graphs

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

adolph
Download Presentation

Graphs

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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 notcomplete then m < 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 < n - 1, G is not connex n = 5 m = 4 n = 5 m = 3

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

  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. • Euler’s 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 vis 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 undirectedgraph 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. • 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.

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

  42. Prim’s algorithm on an example...

More Related