Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs CS 105. 10/02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.

Similar presentations


Presentation on theme: "Graphs CS 105. 10/02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition."— Presentation transcript:

1 Graphs CS 105

2 10/02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition The Graph Data Structure set V of vertices collection E of edges (pairs of vertices in V) Drawing of a Graph vertex circle/oval edge line connecting the vertex pair

3 10/02/05 Graphs Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Sample Uses Airports and flights Persons and acquaintance relationships (e.g., friendster) Intersections and streets Computers and connections; the Internet

4 10/02/05 Graphs Slide 4 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Graph Example Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(a,d)} d a b c

5 10/02/05 Graphs Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Directed Graph Edge set is a set of ordered pairs Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(d,b),(d,a)} d a b c

6 10/02/05 Graphs Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Adjacency Vertices connected by an edge are adjacent An edge e is incident on a vertex v (and vice-versa) if v is an endpoint of e Degree of a vertex v, deg(v) number of edges incident on v if directed graph, indeg(v) and outdeg(v)

7 10/02/05 Graphs Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Graph Properties Graph G with n vertices and m edges  v in G deg(v) = 2m  v in G indeg(v) =  v in G outdeg(v) = m m is O(n 2 ) m <= n(n-1)/2 if G is undirected m <= n(n-1) if G is directed

8 10/02/05 Graphs Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Subgraphs Graph H a subgraph of graph G vertex set of H is a subset of vertex set of G edge set of H is a subset of edge set of G Spanning subgraph vertex set of H identical to vertex set of G

9 10/02/05 Graphs Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Paths and Cycles (simple) Path sequence of distinct vertices such that consecutive vertices are connected through edges (simple) Cycle same as path except that first and last vertices are identical Directed paths and cycles edge directions matter

10 10/02/05 Graphs Slide 10 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Graph Implementations Edge List vertices and edges are nodes edge nodes refer to incident vertex nodes Adjacency List same as edge list but vertex nodes also refer to incident edge nodes Adjacency Matrix 2D matrix of vertices; entries are edges

11 10/02/05 Graphs Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Implementation Examples Illustrate the following graph using the different implementations d a b c 1 2 3 4 5

12 10/02/05 Graphs Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Edge List 15423 ABDC

13 10/02/05 Graphs Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Adjacency List

14 10/02/05 Graphs Slide 14 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Adjacency Matrix

15 10/02/05 Graphs Slide 15 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Comparing Implementations Edge List simplest but most inefficient Adjacency List many vertex operations are O(d) time where d is the degree of the vertex Adjacency Matrix adjacency test is O(1) addition/removal of a vertex: resize 2D array

16 10/02/05 Graphs Slide 16 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Sample Graph problems Connectivity Determine if a given graph is connected; i.e., is there a path between any two vertices in the graph Euler tour Is there a sequence of vertices that traverse all edges in the graph exactly once Hamiltonian cycle Is there a simple cycle that connects all vertices in the graph

17 10/02/05 Graphs Slide 17 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Weighted Graphs Graph G = (V,E) such that there are weights/costs associated with each edge w((a,b)): cost of edge (a,b) representation: matrix entries costs d a b c 20 32 65 12 e 35

18 10/02/05 Graphs Slide 18 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Problems on Weighted Graphs Minimum-cost Spanning Tree Given a weighted graph G, determine a spanning tree with minimum total edge cost Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine the shortest paths from v to all other vertices in G Path length: sum of all edges in path Traveling Salesman Given a weighted graph G, provide shortest cycle that contains all vertices in G


Download ppt "Graphs CS 105. 10/02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition."

Similar presentations


Ads by Google