Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Graphs: Concepts, Representations, and Traversals, Slides of Data Structures and Algorithms

An introduction to graphs, their terminology, applications, and various representations. It covers undirected and directed graphs, their differences, and graph traversals such as depth first search (dfs) and breadth first search (bfs). The document also includes examples and pseudo-code for graph traversals.

Typology: Slides

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

51 documents

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

Download Graphs: Concepts, Representations, and Traversals and more Slides Data Structures and Algorithms in PDF only on Docsity! Algorithm & Data Structures gy, What is a Graph? A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices E lxamp e: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b ) ( d) ( )c ,e , c, , c,e , (d,e)} d e Applications CS16electronic circuits networks (roads, flights, communications) karachi LAXIslamabad Abbott Lahore Maree Muza- rabad Terminology: Adjacent and Incident If (v0 v1) is an edge in an undirected graph , , v0 and v1 are adjacent The edge (v0 v1) is incident on vertices v0 and v1, If <v0, v1> is an edge in a directed graph i dj t t d i dj t fv0 s a acen o v1, an v1 s a acen rom v0 The edge <v0, v1> is incident on v0 and v1 docsity.com Terminology: Degree of a Vertex The degree of a vertex is the number of edges incident to that vertex di d h For recte grap , the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is the number of edges that have v as the tail Examples 0 1 2 3 2 3 3 0 1 2 3 4 5 6 G1 G21 1 1 13 33 directed graph 0 in:1, out: 13 in-degree out-degree 1 in: 1, out: 2 2 in: 1, out: 0 G3 Terminology: Path h fpat : sequence o vertices v1,v2,. . .vk such that consecutive 3 2 vertices vi and vi+1 are adjacent. 3 3 3 a b a b c d e c d e 7 a b e d c b e d c Graphs: Example 1 6 2 3 7 4 5 Legend 8 Directional path Bi-directional paths docsity.com Examples for Adjacency Matrix 0 0 1 2 3 1 0 1 1 0 1 1 1 1 ⎡ ⎢ ⎢ ⎢ ⎤ ⎥ ⎥ ⎥ 0 1 1 0 0 1 ⎡ ⎢ ⎢ ⎢ ⎤ ⎥ ⎥ ⎥21 1 1 1 0 1 1 0⎣ ⎢ ⎦ ⎥ 0 0 0⎣ ⎦ G2G1 0 1 0 2 4 5 1 2 3 3 6 7 0 1 0 1 1 2 3 0 2 3 1 2 0 3 2 3 20 1 3 0 3 3 4 0 1 2 G1 1 2 50 0 1 5 61 0 2 4 6 5 71 2 7 G3 6 G4 2 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes Graph Traversal Problem: Search for a certain node or traverse all nodes in the graph Depth First Search Once a possible path is found, continue the search until the end of the path Breadth First Search St t l th t ti d d i h ar severa pa s a a me, an a vance n eac one step at a time Depth-First Search A B C D E F G H I J K L M N O P docsity.com Exploring a Labyrinth Without Getting Lost A depth first search (DFS) in an undirected graph G is like - wandering in a labyrinth with a string and a can of red paint without getting lost. We start at vertex s, tying the end of our string to the point and painting s “visited”. Next we label s as our current vertex called u. Now we travel along an arbitrary edge (u, v). If edge (u, v) leads us to an already visited vertex v we return to u. If vertex v is unvisited, we unroll our string and move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps. Depth-First Search Algorithm DFS(v); Input: A vertex v in a graph Output: A labeling of the edges as “discovery” edges and “backedges” for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge i l ll ( )recurs ve y ca DFS w else label e as a backedge Breadth-First Search Like DFS, a Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with l f l tisevera use u proper es. The starting vertex s has level 0, and, as in DFS, defines that point as an “anchor.” In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited. These edges are placed into level 1 In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and placed in level 2. This continues until every vertex has been assigned a level. The label of any vertex v corresponds to the length of the shortest path from s to v. 23 BFS - A Graphical R t tiepresen a on A B C D 0 A B C D 0 1 E F G H E F G H b)a) M N O P I J K L M N O P I J K L A C DB 0 1 2 A B C D 0 1 2 3 d)c) E F G H I J K L E F G H 24M N O P I J K L M N O P docsity.com More BFS 0 1 2 3 0 1 2 3 A B C D A B C D E F G H 4 E F G H 4 I J K L I J K L M N O P M N O P 5 BFS Pseudo-Code Algorithm BFS(s): Input: A vertex s in a graph Output: A labeling of the edges as “discovery” edges and “cross edges” i iti li t i L t t i t n a ze con a ner 0 o con a n ver ex s i ← 0 while Li is not empty do create container Li+1 to initially be empty for each vertex v in Li do if edge e incident on v do let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge insert w into Li+1 else label e as a cross edge i i 26 ← + 1 Applications: Finding a Path Find path from source vertex s to destination dvertex Use graph search starting at s and terminating as soon as we reach d Need to remember edges traversed Use depth – first search ? Use breath – first search? DFS vs. BFS E F B A startDFS Process G CD destination DFS on BB DFS on C B C B Return to call on B D Call DFS on D A DFS on A A A A C ll S G f d d ti ti d ! B D a DF on G oun es na on - one Path is implicitly stored in DFS recursion Path is: A, B, D, G A docsity.com