Download presentation
Presentation is loading. Please wait.
1
Graphs Intro G.Kamberova, Algorithms Graphs Introduction Gerda Kamberova Department of Computer Science Hofstra University
2
Graphs Intro G.Kamberova, Algorithms Motivation We have used trees to organize information in a non-linear data structure. Trees are examples of graphs. Graphs are commonly used to model a real-life problems involving finite set entities and the relations between them Examples include –transportation networks –communication networks –parallel computer architectures –circuit design –biological evolution of species –cultural and language relations between communities and peoples –family genealogy
3
Graphs Intro G.Kamberova, Algorithms Overview Motivation Definitions and terminology Graph representation Graph problems and real-life problems they can model
4
Graphs Intro G.Kamberova, Algorithms Directed Graphs Directed graph : G is a pair (V,E), G=(V,E) where Example: V={1,2,3,4,5,6}, E={(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)} 13 6 2 54 G Directed
5
Graphs Intro G.Kamberova, Algorithms Undirected Graphs The edges in E are unordered pairs Undirected graph : G is a pair (V,E), G=(V,E) where Example: V={1,2,3,4,5,6}, E={(1,2), (1,5), (2,5), (3,6)} 13 6 2 54 G Undirected
6
Graphs Intro G.Kamberova, Algorithms Graphs: Weighted, Dense, Sparse G=(V,E) is a weighted graph if weights (costs) are associated with the edges. G=(E,V), m=|E|, n=|V|, then Sparse graph: m much less than Dense graph:]m is close to
7
Graphs Intro G.Kamberova, Algorithms Definitions G=(V,E) is a directed graph, if e=(u,v) is an edge in E, –e is incident from u –e is incident to v –in-degree of v is the number of edges incident to v –out-degree of v is the number of edges incident from v Walk: an alternating sequence of vertices and edges, starting and ending in a vertex, s.t. each edge is incident on the two vertices immediately preceding and following it. Closed walk: begins and ends at the same vertex A path: a walk in which no vertex is repeated; the length of the path is the number edges in it If there is a path from u to v, v is reachable from u Cycle: closed path Acyclic graph: without cycles Connected graph: at least one path exists between every pair vertices
8
Graphs Intro G.Kamberova, Algorithms Graph Representations Adjacency-list representation: – G=(V,E) is represented as an array Adj of lists – one list for each vertex u in V, –The storage needed: O(n+m). –If G is directed –If G is undirected –Advantage: good use of memory for sparse graphs. –Disadvantage: no quick way ( i.e., O(1)), to check if particular edge exists.
9
Graphs Intro G.Kamberova, Algorithms Graph Representations Adjacency-matrix representation: – G=(V,E) is represented as nxn square binary matrix A – one row and one column for each vertex –The storage needed: –If G is undirected, A is symmetric, has zeros on the main diagonal, so enough to store only elements above the main diagonal. –Advantage: O(1) to check if an edge exists –Disadvantage: waste of memory for sparse graphs
10
Graphs Intro G.Kamberova, Algorithms 1 3 45 2 1 2 3 4 5 12345 1 2 3 4 5 2 5 1543 24 32 5 4 21 G Undirected =(V,E) V={1,2,3,4,5}, n=|V|=5, m=|E|=7 E={(1,2),{1,5},(2,4),(2,5),(2,3),(3,4),(4,5)} Adjacency-matrix representation Adjacency-list representation Adj Example: representations of undirected graph
11
Graphs Intro G.Kamberova, Algorithms 1 654 2 1 2 3 4 5 1 2345 1 2 3 4 5 2 5 6 2 4 G Directed =(V,E) V={1,2,3,4,5,6}, n=|V|=6, m=|E|=8 E={(1,2),{1,4},(2,5),(3,5),(3,6),(4,2),(5,4),(6,6)} Adjacency-matrix representation Adjacency-list representation Adj Example: representations of directed graph 3 6 6 6 4 5 6
12
Graphs Intro G.Kamberova, Algorithms Using graphs for modeling Graphs are used to model problems in CS, EE, Chemistry, Operations Research, Political Sci, Economics, Biology, Linguistics,... To model real-life problems –Represent the physical structure of the problems as graphs: every member of the collection becomes a vertex, the edges represent relations between the objects. Transportation networks: airlines, auto-transport, oil pipelines network. –vertices: cities/sites; –edge exists between two cities, if one can get from A to B by a direct flight/link. –can associate weights: money, distance, amount of cargo, time. –Questions: Can one get from any source to any destination? What's the route with minimal stopovers between any two airports? Communication networks: telephone, computers
13
Graphs Intro G.Kamberova, Algorithms Using graphs for modeling Precedence graphs are used to model precedence relations that exist between tasks necessary to accomplish a job. –Each task is a vertex. –A direct edge (u,v) exists if u must be completed before v. –Examples: Plan CS courses to take each semester to completing the degree. Plan daily schedule –make: Modularity and separate compilation in modern programming require keeping track of dependences between modules in software systems In UNIX make manages the complexity of of large programs. Using make one can automatically rebuild an executable version of the big program after some changes have been made in some modules. make uses graph representations and algorithms to do that. –Given a precedence graph a common problem is producing a viable schedule of the tasks involved (may impose additional restrictions)
14
Graphs Intro G.Kamberova, Algorithms Graph Problems Connectivity and connected components –Is G connected? –If not, how many different connected components there are? –In a communication newtoeks: Can every person send a message to every other person? –In a transportation networks: Is it possible that any vehicle be moved between any two locations? Reachability –Can v be reached from u? –In undirected graph, if we have established that G is connected, then done. –In communication networks: Is it possible that one person sends a message to another person? – In transportation networks: Is it possible to move from one location to another? –In evolution: Is a species descendant of another species? Shortest path –Even if we have established, that u is reachable from v, it may be important to find the shortest path from v to u. –Variations of shortest path find the shortest path from a given vertex to all the rest; find the shortest path between all possible pairs. In transport. networks: go from one place to another with min stopovers.
15
Graphs Intro G.Kamberova, Algorithms Graph Problems Spanning trees –A spanning tree is acyclic subset of E that connects (spans) all the vertices of G. –If the graph is weighted one looks for minimum spanning tree (with min total cost). –Application: Design a railroad transportation system that connects all cities on a given map and uses min amount of track. –model the map as undirected graph with weights distances, –find the minimum spanning tree. Given unorganized set of 3D points sampled from a 3D scene, orient the cloud (assign normal to each point) Topological Sorting: a way of solving the scheduling problem on a precedence graph (or establishing that it is unsolvable). –Input: directed acyclic graph, –Output: an ordered list of vertices such that if E contains the edge (u,v), then u appears before v in the list.
16
Graphs Intro G.Kamberova, Algorithms Graph Problems Konigsberg bridge problem (Euler, 1763) -- origin of graph theory. –Pregel river goes through the city. There is an island and after the island the river divides into two. Seven bridges were built to allow people to reach the various land areas –Devise a route that starts from any of the land areas, crosses each bridge exactly once, and returns to the starting point. –Euler elegantly proved that no solution exist by formulating it as a graph problem (land areas are vertices and bridges are edges).
17
Graphs Intro G.Kamberova, Algorithms Konigsberg bridge problem Devise a route that starts from any of the land areas, crosses each bridge exactly once, and returns to the starting point. There is no solution: the problem is that every vertex has odd degree As a graph problem: Find a closed walk that visits every edge exactly once. Graphs for which such walks exist are called Euler graphs. Euler's proof: A graph is an Euler graph iff every vertex has even degree.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.