What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

Graph Theory.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++
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.
Graphs.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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,
Chapter 2 Graph Algorithms.
Graphs Chapter 12.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Graphs1 Definitions Examples The Graph ADT LAX PVD LAX DFW FTL STL HNL.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Applications of Data-Structure
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University Graphs Original Slides by Rada Mihalcea.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Graphs Slides are adopted from “Discrete.
Introduction to Graphs
UCS 406 – Data Structures & Algorithms
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
CHP-7 GRAPH.
Graph Representation, DFS and BFS
Graph & BFS.
Graphs Chapter 13.
Graphs CSE 2011 Winter November 2018.
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.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Graphs Chapter 7 Visit for more Learning Resources.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Graphs.
Graphs.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Graphs.
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:

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

A graph consists G of a set V of elements called nodes ( points or vertices ) a set E of edges such that each e in E is identified with unique unordered pair [u , v] of nodes in v and is denoted by e = [u , v] G = ( V , E ) If e = [ u , v ] then end points of e : u , v Adjacent nodes or neighbor nodes : u , v degree of a node : number of edges in node isolated node : if deg(u) = 0 Path : path p of length n from node u to node v is a sequence of n+1 nodes ( v0, v1, v2, . . . . . , vn) such that u = v0 and v = vn vi-1 is adjacent to vi for I = 1 to n Closed Path : if v0 = vn Simple Path : If all nodes in a path are distinct Cycle : closed path with length 3 or more

K - Cycle : cycle of length k Connected Graph : If there is a path between any two of its node Complete graph : If every node u in G is adjacent to every other node v in G. A complete graph will have n (n-1) /2 edges Tree graph of free tree : Connected Graph without cycle Labeled graphs : if edges are assigned data Weights : non negative numeric value, may be Length of the edge. If no weight is specified then w(e) = 1 is considered Weight of the Path : Sum of weights of all edges in this path Multiple Edges : e1 and e2 are multiple edges if both e1 and e2 = [u , v] Loops : is it has identical end points, e = [ u , u] Multigraph : with multiple edges and loops Finite multigraph : with finite number of edges and nodes

Directed Graph or Digraph : if edges have direction edge is identified by ordered pair of nodes ( u , v ) Arc : Directed Edge Different terminology used for end points : Begin End, Origin and terminal point, predecessor and successor Outdegree of node u : number of edges beginning at u Indegree of node u : number of edges ending at u Source node: +ve outdegree and 0 indegree Sink : +ve indegree and 0 outdegree A node v is reachable from node u if there is a directed path from v to u Strongly connected directed graph : if for each pair of node there is a path from u to v and v to u Unilaterally connected directed graph : if for each pair of node there is a path from u to v or v to u Parallel Edges, simple directed graph

Applications electronic circuits CS16 networks (roads, flights, communications) LAX JFK DFW STL HNL FTL

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

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 have v as the head the out-degree of a vertex v is the number of edges that have v as the tail 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 1 2 3 3 3 3 3 4 5 6 3 G1 3 1 1 1 1 G2 directed graph 1 in: 1, out: 2 in-degree out-degree in:1, out: 1 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 connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.

(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

Oriented (Directed) Graph A graph where edges are directed

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

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, say adj_mat 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 need not be symmetric

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

Merits of Adjacency Matrix From the adjacency matrix, to determine the connection of vertices is easy The degree of a vertex is For a digraph (= directed graph), the row sum is the out-degree, while the column sum is the in-degree

Linked representation(Adjacency List) Each row in adjacency matrix is represented as an adjacency list. 1 2 1 3 1 2 3 2 1 2 3 2 3 1 2 1 1 3 2 1 2 G1 G2

Sequential Representation of Graphs : Adjacency Matrix and Path Matrix W Y Z 1 1 2 1 2 1 2 3 A3 = A1 = A2 = A4 = Number of Paths between Y to W of length 3 are 2 (Y -> Z -> X -> W AND Y->W->Z->W) B4 = A1 + A2 + A3 + A4 entry of matrix B4 gives the number of paths of length 4 or less from node vi to vj entry of matrix Br gives the number of paths of length r or less from node vi to vj

Pij = 1 (if there is path from vi to vj) = 0 (otherwise) Path matrix: Let G be simple directed graph with m nodes (v1, v2, ....... vm ). Then, path matrix or reach-ability matrix of G is: Pij = 1 (if there is path from vi to vj) = 0 (otherwise) B = A1 + A2 + A3 + A4 Gives path matrix P when all non-zero entries are replaced by 1 1 P =

Example of transitive closure: Transitive closure: For a graph G, it is defined to be graph G’ such that it has same nodes as G & there is an edge (vi, vj) in G’ whenever there is path from vi to vj in G. Example of transitive closure: Initial graph Final graph 3 4 2 1 3 4 2 1 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0

Warshall’s Algorithm It is used to find path matrix in efficient way, rather than calculating powers of adjacency matrix. First we define m-square Boolean matrices P0, P1, ...... Pm as follows. Let Pk [i, j] denote the ij entry of matrix Pk. Then, we define: Pk [i, j] = 1 (if there is simple path from vi to vj which use nodes from v1, v2, ....... vk ). = 0 (otherwise)

Warshall’s Algorithm Warshall observed that Pk [i, j] = 1 can occur in following 2 cases: There is simple path from vi to vj which only used nodes from v1, v2, ....... vk-1 , hence Pk-1 [i, j] = 1 There is simple path from vi to vk and from vk to vj which only used nodes from v1, v2, ....... vk-1 , hence Pk-1 [i, k] = 1 and Pk-1 [k, j] = 1 Accordingly: Pk [i, j] = Pk-1 [i, j] v (Pk-1 [i, k] ^ Pk-1 [k, j] )

Finding transitive closure 3 4 2 1 3 4 2 1 R2 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 3 4 2 1 R1 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0 0 R0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 3 4 2 1 R4 0 0 1 0 1 1 1 1 0 0 0 0 R3 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 3 4 2 1

Warshall’s Algorithm A directed graph G with M nodes is maintained in memory by its adjacency matrix A. This algo. finds path matrix P of graph G. Repeat for i, j = 1, 2, ....... M : //[Initializes P] If A[i,j] = 0 then Set P[i,j] = 0 else Set P[i,j] = 1 2. Repeat Steps 3 and 4 for k = 1, 2, ...... M //[updates P] Repeat Step 4 for i = 1, 2, ...... M Repeat for j = 1, 2, ...........M Set P[i,j] = P[i,j] v (P[i,k] ^ P[k,j]) 3. Exit

Shortest Path (Modified Warshall’s algorithm) 7 5 ∞ 12 2 3 4 9 1 7 5 ∞ 2 3 4 1 Q1 = Q0 = 7 5 ∞ 12 2 10 3 4 1 6 7 5 8 6 3 2 9 4 1 7 5 ∞ 12 2 10 3 4 9 1 11 Q3 = Q4 = Q2 =

Shortest Path (Modified Warshall’s algorithm) Graph G is maintained in memory by its weight matrix W = wij defined as: wij = w(e) (if there is an edge from vi to vj) = 0 (otherwise) Therefore, we define matrix Q by modifying Warshall’s algo. as: Qk [i,j] = the smaller of the length of preceding path from vi to vj or = the sum of lengths of preceding paths from vi to vk and from vk to vj Accordingly: Qk [i, j] = Min (Qk-1 [i, j] , Qk-1 [i, k] + Qk-1 [k, j] )

Shortest Path (Modified Warshall’s algorithm) A weighted graph G with M nodes is maintained in memory by its weight matrix W. This algo. finds matrix Q such that Q[i,j] is length of shortest path from node vi to vj. Repeat for i, j = 1, 2, ........ M //[Initializes Q] if W[i,j] = 0 then Set Q[i,j] = Infinity else Set Q[i,j] = W[i,j] 2. Repeat Steps 3 and 4 for k = 1, 2, ...... M //[updates Q] Repeat Step 4 for i = 1, 2, ...... M Repeat for j = 1, 2, ...........M Set Q[i,j] = Min (Q[i,j], Q[i,k] + Q[k,j] ) 3. Exit

Traversing a Graph Systematically examining the nodes and edges of a graph. Two methods Breadth First Search (BFS) Depth First Search (DFS) BFS will use Queue and DFS will use Stack as auxiliary storage The algorithm will process only those nodes which are reachable from the starting node During the execution of the algorithm, each node N of G will be in one of the three states, called status of N STATUS = 1 : Ready State , i.e. initial state of node N STATUS = 2 : Waiting State , i.e. node N is in Queue or stack waiting to be processed STATUS = 3 : Processed State , i.e. node N has been processed

Breadth-First Search: Initialize all nodes to ready state , STATUS = 1 Put the starting node A in Queue and change its status to waiting, STATUS = 2 Repeat step 4 and 5 until Queue is empty Remove the front node N of Queue. Process N and change STATUS of N to processed, STATUS = 3 Add to the rear of Queue all neighbors of N that are in ready state (STATUS = 1) and change their STATUS to waiting state ( STATUS = 2 ) Exit

STEP QUEUE ORIGIN F R 1 A 2 AFCB 0AAA 4 3 AFCBD 0AAAF 5 AFCBDG 0AAAFB Node Adj List A F, C , B B G, C C F D E D, C, J G C, E J D, K K E, G STEP QUEUE ORIGIN F R 1 A 2 AFCB 0AAA 4 3 AFCBD 0AAAF 5 AFCBDG 0AAAFB 6 7 AFCBDGE 0AAAFBG 8 AFCBDGEJ 0AAAFBGJ 9 … .. 10 ….

Applications of Breadth First Search Shortest Path and Minimum Spanning Tree for un-weighted graph Peer to Peer Networks. Crawlers in Search Engines Social Networking Websites GPS Navigation systems Broadcasting in Network In Garbage Collection

Depth-First Search: Initialize all nodes to ready state , STATUS = 1 Push the starting node A in STACK and change its status to waiting, STATUS = 2 Repeat step 4 and 5 until STACK is empty Pop the TOP node N of STACK. Process N and change STATUS of N to processed, STATUS = 3 Push onto STACK all neighbors of N that are in ready state (STATUS = 1) and change their STATUS to waiting state ( STATUS = 2 ) Exit

Iterative Recursive

1 A 2 FCB AB 3 FCG ABG 4 FCE ABGE 5 FCDJ ABGEJ 6 … Node Adj List A G, C C F D E D, C, J G C, E J D, K K E, G STEP STACK POP 1 A 2 FCB AB 3 FCG ABG 4 FCE ABGE 5 FCDJ ABGEJ 6 …

Applications of Depth First Search Finding minimum spanning tree Detecting cycle in a graph Path Finding Topological Sorting Solving puzzles with only one solution

Linked Representation of Graph

A B C D E Node Adjacency List A B, C, D B C D C, E E

Sequential representation of graph G in memory using adjacency matrix A has some drawbacks. It is difficult to insert/delete nodes in G (since size of A needs to be changed & nodes to be reordered). Matrix A may be sparse (containing many zeros for no edges) in which great deal of memory is wasted.

Operation on Graph Searching: Find(info, link, start, item, loc) Finds the location loc of first node containing item, or sets loc = NULL Set Ptr = start Repeat while Ptr != NULL If item = info[Ptr], then Set loc = ptr and return else Set Ptr = link[Ptr] 3. Set loc = NULL and return

Delete(info, link, start, avail, item, flag) Deleting: Delete(info, link, start, avail, item, flag) Deletes first node in list containing item, or sets flag=False when item doesn’t appear in list. [List empty?] If start = NULL, then Set flag = False and return [item in first node?] If info[start] = item, then Set Ptr = start, start = link[start] link[Ptr] = avail, avail = Ptr flag = true and return Set Ptr = link[start] and save = start [initializes pointers] Repeat steps 5 and 6 while Ptr != NULL If Info[Ptr] = item, then Set link[save] = link[Ptr], link[Ptr] = avail avail = Ptr, flag = True and return Set save = Ptr and Ptr = link[Ptr] [updates pointers] Set flag = False and return

InsNode(node, next, Adj, start, availN, N, flag) Inserting: InsNode(node, next, Adj, start, availN, N, flag) This procedure inserts node N in graph G. [Overflow?] If availN = NULL, then Set flag = False and return Set Adj[availN] = NULL [node is isolated] [Removes node from availN list] Set New = availN and availN = next[availN] [Inserts node N in node list] Set node[New] = N, next[New] = start, start = New 5. Set flag = True and return.