Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi

Slides:



Advertisements
Similar presentations
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Chapter 8, Part I Graph Algorithms.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
Graphs1 Definitions Examples The Graph ADT LAX PVD LAX DFW FTL STL HNL.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
COSC 2007 Data Structures II
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University Graphs Original Slides by Rada Mihalcea.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs A New Data Structure
Elementary Graph Algorithms
Data Structures Graphs - Terminology
Data Structures 13th Week
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
Graphs.
Graph Representation, DFS and BFS
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Chapter 22: Elementary Graph Algorithms I
Elementary Graph Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Algorithms: Design and Analysis
Graphs Chapter 7 Visit for more Learning Resources.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPHS.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi

What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), 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 Example: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c d e

Applications electronic circuits CS16 networks (roads, flights, communications)

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 v0 is adjacent to v1, and v1 is adjacent from v0 The edge <v0, v1> is incident on v0 and v1

Directed vs. Undirected Graph An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0) A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1,v0> tail head

Oriented (Directed) Graph A graph where edges are directed

Terminology: Degree of a Vertex The degree of a vertex is the number of edges incident to that vertex For directed graph, the in-degree of a vertex v is the number of edges that going into it the out-degree of a vertex v is the number of edges that have out from it. if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is Why? Since adjacent vertices each count the adjoining edge, it will be counted twice

Examples 3 2 1 2 3 3 3 1 2 3 3 4 5 6 3 G1 1 1 G2 1 1 3 in:1, out: 1 directed graph in-degree out-degree 1 in: 1, out: 2 2 in: 1, out: 0 G3

Terminology: Path path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 3 2 3 3 3 a b a b c c d e d e a b e d c b e d c

More Terminology simple path: no repeated vertices cycle: simple path, except that the last vertex is the same as the first vertex a b b e c c d e

Even More Terminology connected graph: any two vertices are connected by some path connected not connected subgraph: subset of vertices and edges forming a graph

(b) Some of the subgraph of G3 Subgraphs Examples 1 2 3 1 2 3 G1 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 1 2 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 G3

More… tree - connected graph without cycles forest - collection of trees

Connectivity Let n = #vertices, and m = #edges A complete graph: one in which all pairs of vertices are adjacent How many total edges in a complete graph? Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice! Therefore, intuitively, m = n(n -1)/2. Therefore, if a graph is not complete, m < n(n -1)/2

More Connectivity n = #vertices m = #edges For a tree m = n - 1 If m < n - 1, G is not connected

Graph Representations The following two are the most commonly used to represent the graph: Adjacency Matrix Adjacency lists

Adjacency Matrix Let G=(V,E) be a graph with n vertices. The adjacency matrix of G is a two-dimensional n by n array. If the edge (vi, vj) is in E(G), adj_mat[i][j]=1 If there is no such edge in E(G), adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph (= directed graph), need not be symmetric

Examples for Adjacency Matrix 1 2 3 4 5 6 7 1 2 3 1 2 G2 G1 symmetric G4

Adjacency Lists An Array of linked lists is used. The size of array =number of vertices. In the adjacency list represent the graph as array of list, each list contains a list of all vertices adjacent to this vertix.

1 2 3 4 5 6 7 1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 7 1 2 2 3 3 1 3 3 1 2 1 2 5 G1 4 6 1 2 1 5 7 2 1 6 G4 G3 2

Graph Traversal Problem: Search for a certain node or traverse all nodes in the graph Depth First Search(DFS) Once a possible path is found, continue the search until the end of the path Breadth First Search(BFS) Start several paths at a time, and advance in each one step at a time

A depth-first search (DFS) 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.

DFS Using stack( pop, push operations) to keep track for ordered visited nodes Push the node to the stack Add it to the output sequence mark it as visited and then check the top of the stack and all unvisited adjacent nodes one by one. If all the adjacent nodes are visited, we pop this node

DFS vs. BFS DFS Process Result is: A, B ,C, D, G, F, E D C B B B A A A start DFS Process E G D C D Call DFS on D C DFS on C B DFS on B B B Return to call on B A DFS on A A A A E F A B D G A B D G A B D G Call DFS on G Result is: A, B ,C, D, G, F, E

DFS application Find a connected components. Solving puzzles with only one solution, such as mazes.

Breadth-First Search(BFS) a Breadth-First Search (BFS) traverses a connected component of a graph. The starting vertex s has level 0 and defines that point as an “anchor.” In the first round, 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 are visited and placed in level 2. This continues until every vertex has been assigned as visited.

BFS Using queue( enqueue,dequeue operations) to keep track for ordered unvisited nodes of currently worked node. mark the first node as visited Check all unvisited adjacent nodes and Enqueue all of them to the queue. Add it to the output sequence and then update the current worked node (check the first element of the queue)and dequeue it directly. Check again all adjacent nodes and enqueu unvisited nodes and so on ….

DFS vs. BFS BFS Process Result is: A, B ,C, D, G, F, E D A B D C E G F start E BFS Process G D C rear front rear front rear front D Dequeue C Nothing to add front rear A B D C Initial call to BFS on A Add A to queue Dequeue A Add B Dequeue B Add C, D E Dequeue F Nothing to add front rear G Dequeue D Add G front rear F Dequeue G Add F,E front rear E Dequeue E Nothing to add front rear E Result is: A, B ,C, D, G, F, E

BFS applications Find the shortest path between two nodes.

Space complexity of DFS is lower than that of BFS. Time complexity of both is same O(|V|+|E|).