Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 341 Lecture 21 Graphs (Introduction)

Similar presentations


Presentation on theme: "CMSC 341 Lecture 21 Graphs (Introduction)"— Presentation transcript:

1 CMSC 341 Lecture 21 Graphs (Introduction)
Prof. Neary Based on slides from previous iterations of this course (Dr. Katherine Gibson)

2 Basic Graph Definitions
A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v, w) where v, w  V V and E are sets, so each vertex v  V is unique, and each edge e  E is unique. Edges are sometimes called arcs or lines Vertices are sometimes called nodes or points UMBC CMSC 341 Graphs (Introduction)

3 UMBC CMSC 341 Graphs (Introduction)
Graph Example A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route 849 PVD 1843 ORD 142 SFO LGA 1743 802 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA UMBC CMSC 341 Graphs (Introduction)

4 UMBC CMSC 341 Graphs (Introduction)
Graph Applications Graphs can be used to model a wide range of applications including Intersections and streets within a city Roads/trains/airline routes connecting cities/countries Computer networks Electronic circuits What else? UMBC CMSC 341 Graphs (Introduction)

5 UMBC CMSC 341 Graphs (Introduction)
Edge Types Directed edge Ordered pair of vertices (u,v) First vertex u is the origin Second vertex v is the destination (e.g., a flight) Undirected edge Unordered pair of vertices (u,v) (e.g., a flight route) flight AA 1206 ORD PVD 849 miles ORD PVD UMBC CMSC 341 Graphs (Introduction)

6 UMBC CMSC 341 Graphs (Introduction)
Graph Types Directed graph All the edges are directed (edges are ordered pairs) (e.g., route network) Sometimes called a digraph Undirected graph All the edges are undirected (unordered pairs) (e.g., flight network) Sparse and dense graphs “Few” and “many” edges, respectively UMBC CMSC 341 Graphs (Introduction)

7 Example: Undirected Graph
All edges are two-way Edges are unordered pairs V = { 1, 2, 3, 4, 5 } E = { (1,2), (2,3), (3,4), (2,4), (4,5), (5,1) } 2 3 1 5 4 UMBC CMSC 341 Graphs (Introduction)

8 Example: Directed Graph
All edges are “one-way” (as shown by arrows) Edges are ordered pairs V = { 1, 2, 3, 4, 5 } E = { (1,2), (3,2), (4,3), (2,4), (4,5), (5,1), (5,4) } 2 3 1 5 4 UMBC CMSC 341 Graphs (Introduction)

9 Graph Terminology and Vocabulary

10 UMBC CMSC 341 Graphs (Introduction)
Terminology End vertices (or endpoints) of an edge U and V are the endpoints of a Edges incident on a vertex a, d, and b are incident on V Adjacent vertices U and V are adjacent Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j UMBC CMSC 341 Graphs (Introduction)

11 UMBC CMSC 341 Graphs (Introduction)
More Terminology Path Sequence of alternating vertices and edges Begins with a vertex Ends with a vertex Each edge is preceded and followed by its endpoints Simple path Path such that all its vertices and edges are distinct Examples P1=(V,X,Z) is a simple path P2=(U,W,X,Y,W,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y UMBC CMSC 341 Graphs (Introduction)

12 UMBC CMSC 341 Graphs (Introduction)
More Terminology Cycle Circular sequence of alternating vertices and edges Each edge is preceded and followed by its endpoints Acyclic: without a cycle Simple cycle Cycle such that all its vertices and edges are distinct Examples C1=(V,X,Y,W,U,V) is simple C2=(U,W,X,Y,W,V,U) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y UMBC CMSC 341 Graphs (Introduction)

13 UMBC CMSC 341 Graphs (Introduction)
Vocabulary Vertex w is adjacent to vertex v if and only if (v, w)  E. For undirected graphs, with edge (v, w), and hence also (w, v), w is adjacent to v and v is adjacent to w. An edge may also have: Weight or cost -- an associated value Label -- a unique name The degree of a vertex, v, is the number of vertices adjacent to v. Degree is also called valence. UMBC CMSC 341 Graphs (Introduction)

14 UMBC CMSC 341 Graphs (Introduction)
Degrees For directed graphs vertex w is adjacent to vertex v if and only if (v, w)  E In-degree of a vertex w is the number of edges (v,w) X has in-degree 3 Out-degree of a vertex w is the number of edges(w,v) X has out-degree 2 X U V W Z Y a c b e d f g h i j UMBC CMSC 341 Graphs (Introduction)

15 UMBC CMSC 341 Graphs (Introduction)
Connectedness An undirected graph is connected if there is a path from every vertex to every other vertex A directed graph is strongly connected if there is a path from every vertex to every other vertex A directed graph is weakly connected if there would be a path from every vertex to every other vertex, disregarding the direction of the edges A complete graph is one in which there is an edge between every pair of vertices A connected component of a graph is any maximal connected subgraph. Connected components are sometimes simply called components UMBC CMSC 341 Graphs (Introduction)

16 UMBC CMSC 341 Graphs (Introduction)
Graph Algorithms Graph Traversal Breadth First Search (BFS) Depth First Search (DFS) Uniform Cost Search (UCS) A/A* Search Etc... Shortest Paths Bellman-Ford Dijsktra’s Floyd-Warshall UMBC CMSC 341 Graphs (Introduction)

17 UMBC CMSC 341 Graphs (Introduction)
Graph Traversal Depth First Search (DFS) DFS(v): Mark v as ”visited” For each edge v -> u: DFS(u) (could use a stack instead of recursion) UMBC CMSC 341 Graphs (Introduction)

18 UMBC CMSC 341 Graphs (Introduction)
Graph Traversal Breadth First Search BFS(v): Initialize a queue Q Mark v as visited, and enqueue v While Q is not empty: w = Q.dequeue() For each edge w -> u: If u is not visited, mark it as visited and enqueue it UMBC CMSC 341 Graphs (Introduction)

19 A Graph ADT Has some data elements Has some operations
Vertices and Edges Has some operations getDegree( u ) -- Returns the degree of vertex u (outdegree of vertex u in directed graph) getAdjacent( u ) -- Returns a list of the vertices adjacent to vertex u (list of vertices that u points to for a directed graph) isAdjacentTo( u, v ) -- Returns TRUE if vertex v is adjacent to vertex u, FALSE otherwise. Has some associated algorithms to be discussed. 8/3/2007 UMBC CMSC 341 Graphs

20 Adjacency Matrix Implementation
Uses array of size |V|  |V| where each entry (i ,j) is boolean TRUE if there is an edge from vertex i to vertex j FALSE otherwise store weights when edges are weighted Very simple, but large space requirement = O(|V|2) Appropriate if the graph is dense. Otherwise, most of the entries in the table are FALSE. For example, if a graph is used to represent a street map like Manhattan in which most streets run E/W or N/S, each intersection is attached to only 4 streets and |E| < 4*|V|. If there are intersections, the table has 9,000,000 entries of which only 12,000 are TRUE. 8/3/2007 UMBC CMSC 341 Graphs

21 Undirected Graph / Adjacency Matrix
2 1 3 4 5 8/3/2007 UMBC CMSC 341 Graphs

22 Directed Graph / Adjacency Matrix
1 5 2 3 4 8/3/2007 UMBC CMSC 341 Graphs

23 Weighted, Directed Graph / Adjacency Matrix
1 8 2 2 5 6 5 7 2 4 and 1 are adjacent to 5. (5,1) and (5,4) have weights of 8 and 5 respectively. 2 is not adjacent to 4, but 4 is adjacent to 2 and edge (2,4) has weight of 6. 3 4 3 8/3/2007 UMBC CMSC 341 Graphs

24 Adjacency Matrix Performance
Storage requirement: O(|V|2 ) Performance: getDegree ( u ) O(|V|) isAdjacentTo( u, v ) O(1) getAdjacent( u ) Storage: O(|V|^2) getDegree(u) O(|V|) -- Returns outdegree of u. Must look at (|V|-1) elements on the u-th row getAdjacent(u) O(|V|) -- Returns list of vertices that are adjacent to u. must look down the u-th row isAdjacentTo(u,v) O(1) -- random access to element[u,v] 8/3/2007 UMBC CMSC 341 Graphs

25 Adjacency List Implementation
If the graph is sparse, then keeping a list of adjacent vertices for each vertex saves space. Adjacency Lists are the commonly used representation. The lists may be stored in a data structure or in the Vertex object itself. Vector of lists: A vector of lists of vertices. The i- th element of the vector is a list, Li, of the vertices adjacent to vi. If the graph is sparse, then the space requirement is O( |E| + |V| ), “linear in the size of the graph” If the graph is dense, then the space requirement is O( |V|2 ) 8/3/2007 UMBC CMSC 341 Graphs

26 Vector of Lists 5 2 3 4 8 1 6 7 1 2 2 4 3 2 4 3 5 5 1 4 8/3/2007 UMBC CMSC 341 Graphs

27 Adjacency List Performance
Storage requirement: O(|V| + |E|) Performance: O getDegree( u ) isAdjacentTo( u, v ) getAdjacent( u ) Storage: O(|V| + |E|) getDegree(u) -- O(D(u)) – Returns outdegree of u. Requires counting list length in each case except when degree is kept in a separate vector which is then O(1) getAdjacent(u) -- O(D(u)) –Returns list of vertices that are adjacent to u. Requires construction of a list of each vertex on the adjacency list of vertex u isAdjacentTo(u,v) - O(D(u)) -- Requires searching the adjacency list of vertex u for the presence of vertex v to see if v is adjacent to u storage -- O(|V|+|E|) for “Array of Lists”-- entry for each vertex and list of entries for each adjacent vertex 8/3/2007 UMBC CMSC 341 Graphs


Download ppt "CMSC 341 Lecture 21 Graphs (Introduction)"

Similar presentations


Ads by Google