Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 341 Lecture 20.

Similar presentations


Presentation on theme: "CMSC 341 Lecture 20."— Presentation transcript:

1 CMSC 341 Lecture 20

2 Announcements

3 Basic Graph Definitions
A graph G = (V,E) consists of a finite set of vertices, V, and a 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. A directed graph is a graph in which the edges are ordered pairs. That is, (u,v)  (v,u), u, v  E. Directed graphs are sometimes called digraphs. An undirected graph is a graph in which the edges are unordered pairs. That is, (u,v) = (v,u). Show examples of directed graph undirected graph

4 a a b e b e c d c d undirected graph directed graph

5 Basic Graph Definitions (cont.)
Vertex v is adjacent to vertex w if and only if (v,w)  E. (Book calls this adjacent from) Vertex v is adjacent from vertex w if and only if (w,v)  E. An edge may also have: weight or cost -- an associated value label -- a unique name The degree of a vertex u in an undirected graph is the number of vertices adjacent to u. Degree is also called valence. The indegree (outdegree) of a vertex u in a directed graph is the number of vertices adjacent to (from) u. Does it matter in an undirected graph? No. Give examples: adjacent to (in undirected: a is adjacent to b but not d) adjacent from (in directed: a is adjacent to b, adjacent from e, but neither adjacent to/from d) degree (in undirected: a has degree 2) indegree (in directed: a has indegree 1 and b has indegree 1) outdegree (in directed: a has outdegree 1, b has outdegree 1

6 Paths in Graphs A path in a graph is a sequence of vertices w1, w2, w3, …, wn such that (wi, wi+1)  E for 1  i < n. The length of a path in a graph is the number of edges on the path. The length of the path from a vertex to itself is 0. A simple path is a path such that all vertices are distinct, except that the first and last may be the same. A cycle in a graph is a path w1, w2, w3, …, wn , w  V such that: there are at least two vertices on the path w1 = wn (the path starts and ends on the same vertex) if any part of the path contains the subpath wi, wj, wi, then each of the edges in the subpath is distinct. A simple cycle is one in which the path is simple. Example: simple path (in undirected: from a to d there are two simple paths of length 2 (abd and aed) and a path of length 3 (abcd) (in undirected: from a to d there is one simple path (abd) simple cycle(in undirected: abdea, bdcb. The cycle abcdba is not simple) (in directed: abdea bdcb. The cycle vdcbdeab is not simple)

7 Connectedness in Graphs
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.

8 a b g c f h d e j i Is it strongly connected? -- NO
Strongly connected components: g hij bacf d e Weakly connected component: whole graph e j i

9 A Graph ADT Has some data elements Has some operations vertices edges
getDegree(u) -- returns the degree of vertex u (undirected graph) getInDegree(u) -- returns the indegree of vertex u (directed graph) getOutDegree(u) -- returns the outdegree of veretx u (directed graph) getAdjacent(u) -- returns a list of the vertices adjacent from a vertex u (directed and undirected graphs) isConnected(u,v) -- returns TRUE if vertices u and v are connected, FALSE otherwise (directed and undirected graphs)

10 Graph Traversals Like trees, can be traversed breadth-first or depth-first. Use stack for depth-first traversal. Use queue for breadth-first traversal. Unlike trees, need to specifically guard against repeating a path from a cycle. Can mark each vertex as “visited” when we encounter it and not consider visited vertices more than once.

11 Breadth-First Traversal
Queue q = new Queue(); graphvertex u; for all v, d[v] =  // mark each vertex unvisited q.enqueue(startvertex); // start with any vertex d[startvertex] = 0; // mark visited while (!q.isEmpty()) { u = q.dequeue(); for (each vertex w adjacent from u) if (d[w] == ) { // w not marked as visited d[w] = d[u]+1; // mark visited q.enqueue(w); }

12 v3 v1 v2 v5 v4 Traversal 0: marks all unvisited 1: marks v1 as visited
2. Enqueue and mark v2, v3 3. Dequeue v2, enqueue and mark v4 4. Dequeue v3 -- no op 5. Dequeue v4 -- no op BFS order: Is v1 reachable from v2 ? Yes if v2 does not end up with  mark after BFS from v1 What are minimum number of vertices that must be visited on a path from v1 to vk? D[vk] v4

13 Depth First Traversal dfs(Graph G) { for (each v  V) dfs(v) }
dfs(Vertex v) { markVisited(v); for(each vertex w adjacent from u) if ( w is not marked as visited) dfs(w)

14 v3 v1 v2 v5 Traversal 0: 1: 2. 3. 4. 5. DFS order: v4

15 DFS (stack version) Stack s = new Stack(); GraphVertex u;
GraphVertex startvertex = graph.getStartVertex(); s.push(startvertex); markVisited(startvertex); while (!s.isEmpty()) { u = s.Pop(); for (each vertex w adjacent to u) if (w is not marked as visited) { markVisited(w); s.push(w); } Edges traversed form a tree which includes all visited vertices in the graph. This is called a spanning tree.

16 Unweighted Shortest Path Problem
Unweighted shortest-path problem: Given as input an unweighted graph, G = (V,E), and a distinguished vertex, s, find the shortest unweighted path from s to every other vertex in G. After running BFS algorithm with s as starting vertex, the shortest path length from s to i is given by d[i].

17 Weighted Shortest Path Problem
Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. Use Dijkstra’s algorithm keep tentative distance for each vertex giving shortest path length using vertices visited so far keep vertex before this vertex (to allow printing of path) at each step choose the vertex with smallest distance among the unvisited vertices (greedy algorithm)

18 Dijkstra’s Algorithm Vertex v, w; start.dist = 0; for (;;) {
v = smallest unknown distance vertex; if (v == NOT_A_VERTEX) break; v.known = TRUE; for each w adjacent to v if (!w.known) if (v.dist + cvw < w.dist) { decrease (w.dist to v.dist + cvw); w.path = v; }

19 3 a b g 1 3 1 2 7 5 3 c f h d 1 4 4 Is it strongly connected? -- NO Strongly connected components: g hij bacf d e Weakly connected component: whole graph 2 e j i 1


Download ppt "CMSC 341 Lecture 20."

Similar presentations


Ads by Google