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

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

Lecture 15. Graph Algorithms
Lecture 5 Graph Theory. Graphs Graphs are the most useful model with computer science such as logical design, formal languages, communication network,
22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Applications Data Structures and Algorithms (60-254)
Chapter 8, Part I Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Data Structures Using C++
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
Social Media Mining Graph Essentials.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
IS 2610: Data Structures Graph April 5, 2004.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 2 Graph Algorithms.
Graph Theory Topics to be covered:
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Spring 2015 Lecture 11: Minimum Spanning Trees
Foundations of Discrete Mathematics
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Data Structures & Algorithms Graphs
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Minimum Spanning Trees
Graph theory Definitions Trees, cycles, directed graphs.
Lecture 12 Algorithm Analysis
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph Algorithm.
Chapter 13 Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Lecture 12 Algorithm Analysis
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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

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

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

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) = n(n − 1)/2

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

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

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

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 n  5 m  (5 

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 Connectivity n  5 m  4 n  5 m  3

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

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?

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

More definitions Oriented graph: each edge go only in one direction Acyclic oriented graph Without cycle With cycle

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

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.

Example 2 in an oriented graph

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.

Example 1

Example 2

They explore the vertices that are reachable starting from a source vertex. Depth-First Search Breadth-First Search (level-order search) Exploration 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.

BSF algorithm

Example

The resulting BSF spanning tree

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.

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

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)

Example 1) 2) 3)4)

5)6)

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 undirected graph with minimum total weight. Example The weight of the MST: W(MST) = = 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 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

Prim’s algorithm on an example...

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.

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.

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