Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.

Similar presentations


Presentation on theme: "1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology."— Presentation transcript:

1 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005 ITS033 – Programming & Algorithms

2 2 ITS033 Topic 01-Problems & Algorithmic Problem Solving Topic 01 - Problems & Algorithmic Problem Solving Topic 02 – Algorithm Representation & Efficiency Analysis Topic 03 - State Space of a problem Topic 04 - Brute Force Algorithm Topic 05 - Divide and Conquer Topic 06-Decrease and Conquer Topic 06 - Decrease and Conquer Topic 07 - Dynamics Programming Topic 08 - Transform and Conquer Topic 09 - Graph Algorithms Topic 10 - Minimum Spanning Tree Topic 11 - Shortest Path Problem Topic 12 - Coping with the Limitations of Algorithms Power http://www.siit.tu.ac.th/bunyarit/its033.php and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7 Midterm

3 3 Outline and Reading Graphs ()  Definition  Applications  Terminology  Properties  ADT Graphs representation  Edge list structure  Adjacency list structure  Adjacency matrix structure

4 4 Graph Algorithms: Introduction Lecture 09.1 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005 ITS033 – Programming & Algorithms

5 5 Graphs are made of dots connected by lines. Graphs are very powerful tools for creating mathematical models of a wide variety of situations. Graph Theory is the branch of mathematics that involves the study of graphs. Graph theory has been used to find the best way to route and schedule airplanes and invent a secret code that no one can crack. Basic Terminology Graph

6 6 A graph is a pair of sets (V, E) where V is the vertex-set and E the edge-set is a family of pairs (possibly directed) of V.

7 7 Graph A graph is a pair (V, E), where A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs of vertices, called edges  Vertices and edges are positions and store elements Example: 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 ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

8 8 Basic Terminology Vertex " Vertex" is a synonym for a node of a graph, i.e., one of the points on which the graph is defined and which may be connected by graph edges

9 9 Edge Types Directed edge 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 Undirected edge  unordered pair of vertices (u,v)  e.g., a flight route Directed graph Directed graph  all the edges are directed  e.g., route network Undirected graph Undirected graph  all the edges are undirected  e.g., flight network ORDPVD flight AA 1206 ORDPVD 849 miles

10 10 Basic Terminology: Degree The degree of a vertex of G is the number of edges incident to it. What is a degree of each vertex for a complete graph with 7 vertices ?

11 11 Basic Terminology Indegree The number of inward directed graph edges from a given graph vertex in a directed graph. Outdegree The number of outward directed graph edges from a given graph vertex in a directed graph.

12 12 Path A path in a graph is a connecting set of (V,E) from an original vertex to a destination vertex.

13 13 P1P1 Terminology (cont.) Path 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 Simple path  path such that all its vertices and edges are distinct Examples Examples  P 1 =(V,b,X,h,Z) is a simple path  P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple XU V W Z Y a c b e d f g hP2P2

14 14 Terminology (cont.) Cycle Cycle  circular sequence of alternating vertices and edges  each edge is preceded and followed by its endpoints Simple cycle Simple cycle  cycle such that all its vertices and edges are distinct Examples Examples  C 1 =(V,b,X,g,Y,f,W,c,U,a,  ) is a simple cycle  C 2 =(U,c,W,e,X,g,Y,f,W,d,V,a,  ) is a cycle that is not simple C1C1 XU V W Z Y a c b e d f g hC2C2

15 15 Basic Terminology A graph is called complete when every pair of vertices is an edge. The complete graph has undirected edges, (binomial coefficient)binomial coefficient

16 16 Connected Graph A connected graph has a path from every vertex to every other

17 17 Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components

18 18 Graph Variations A weighted graph associates weights with either the edges or the vertices A weighted graph associates weights with either the edges or the vertices  E.g., a road map: edges might be weighted w/ distan

19 19 Subgraphs A subgraph S of a graph G is a graph such that  The vertices of S are a subset of the vertices of G  The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph

20 20 Trees and Forests A (free) tree is an undirected graph T such that  T is connected  T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest

21 21 Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

22 22 Applications Electronic circuits  Printed circuit board  Integrated circuit Computer networks  Local area network  Internet  Web Databases  Entity-relationship diagram

23 23 Applications Transportation networks  Highway network  Flight network

24 24 Terminology Graph Graph Vertex Vertex Edge, Directed Edge, undirected edge Edge, Directed Edge, undirected edge Path, Simple Path Path, Simple Path Cycle, Simple Cycle Cycle, Simple Cycle Degree, Indegree, Outdegree Degree, Indegree, Outdegree Complete Graph Complete Graph Connected Graph Connected Graph Weighted Graph Weighted Graph Directed Graph Directed Graph Undierected graph Undierected graph Subgraph Subgraph Spanning tree Spanning tree

25 25 Graph Algorithms: Representation Lecture 09.2 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005 ITS033 – Programming & Algorithms

26 26 Representing Graphs with Adjacency Matrix Assume V = {1, 2, …, n} Assume V = {1, 2, …, n} An adjacency matrix represents the graph as a n x n matrix A: An adjacency matrix represents the graph as a n x n matrix A:  A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E

27 27 Graphs: Adjacency Matrix Example: 1 24 3 a d bc 1234 1 2 3 ?? 4 from to

28 28 Graphs: Adjacency Matrix Example: 1 24 3 a d bc 1234 10110 20010 30000 40010 from to

29 29 An entry in row i and column j corresponds to the edge e = (v,, v j ). Its value is the weight of the edge, or -1 if the edge does not exist. Adjacency Matrix (a) D A C E B from to

30 30 An entry in row i and column j corresponds to the edge e = (v,, v j ). Its value is the weight of the edge, or -1 if the edge does not exist. Adjacency Matrix - weighted (b) D B A E C 4 2 7 3 6 4 1 2 from to

31 31 Adjacency List Adjacency list: for each vertex v  V, store a list of vertices adjacent to v Example:  Adj[1] = {2,3}  Adj[2] = {3}  Adj[3] = {}  Adj[4] = {3} Variation: can also keep a list of edges coming into vertex 1 24 3

32 32 Adjacency Lists

33 33 Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph  Pick a vertex as the root  Choose certain edges to produce a tree  Note: might also build a forest if graph is not connected

34 34 Graph Algorithms: Depth First Search Lecture 09.3 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005 ITS033 – Programming & Algorithms DB A C E

35 35 Outline and Reading Depth-first search  Algorithm  Example  Properties  Analysis Applications of DFS  Path finding  Cycle finding

36 36 Depth-First Search Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. Intuitively, one starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

37 37 Depth-First Search Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring.

38 38 Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G  Visits all the vertices and edges of G  Determines whether G is connected  Computes the connected components of G  Computes a spanning forest of G

39 39 Depth-First Search DFS on a graph with n vertices and m edges takes O(n  m ) time DFS can be further extended to solve other graph problems  Find and report a path between two given vertices  Find a cycle in the graph Depth-first search is to graphs what Euler tour is to binary trees

40 40 DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK)

41 41 Example DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

42 42 Example (cont.) DB A C E DB A C E DB A C E D B A C E

43 43 DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze  We mark each intersection, corner and dead end (vertex) visited  We mark each corridor (edge ) traversed  We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

44 44 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v DB A C E

45 45 Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice  once as UNEXPLORED  once as VISITED Each edge is labeled twice  once as UNEXPLORED  once as DISCOVERY or BACK Method incidentEdges is called once for each vertex DFS runs in O(n  m) time provided the graph is represented by the adjacency list structure  Recall that  v deg(v)  2m

46 46 Path Finding We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern We call DFS(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex and the current vertex As soon as destination vertex z is encountered, we return the path as the contents of the stack Algorithm pathDFS(G, v, z) setLabel(v, VISITED) S.push(v) if v  z return S.elements() for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) S.push(e) pathDFS(G, w, z) S.pop(e) else setLabel(e, BACK) S.pop(v)

47 47 Cycle Finding We can specialize the DFS algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w Algorithm cycleDFS(G, v, z) setLabel(v, VISITED) S.push(v) for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) S.push(e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) pathDFS(G, w, z) S.pop(e) else T  new empty stack repeat o  S.pop() T.push(o) until o  w return T.elements() S.pop(v)

48 48 Graph Algorithms: Breadth First Search Lecture 09.4 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th http://www.siit.tu.ac.th/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005 ITS033 – Programming & Algorithms CB A E D L0L0 L1L1 F L2L2

49 49 Outline and Reading Breadth-first search  Algorithm  Example  Properties  Analysis  Applications DFS vs. BFS  Comparison of applications  Comparison of edge labels

50 50 Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G  Visits all the vertices and edges of G  Determines whether G is connected  Computes the connected components of G  Computes a spanning forest of G

51 51 Breadth-First Search BFS on a graph with n vertices and m edges takes O(n  m ) time BFS can be further extended to solve other graph problems  Find and report a path with the minimum number of edges between two given vertices  Find a simple cycle, if there is one

52 52 BFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G, s) L 0  new empty sequence L 0.insertLast(s) setLabel(s, VISITED ) i  0 while  L i.isEmpty() L i  1  new empty sequence for all v  L i.elements() for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY ) setLabel(w, VISITED ) L i  1.insertLast(w) else setLabel(e, CROSS ) i  i  1 Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u  G.vertices() setLabel(u, UNEXPLORED ) for all e  G.edges() setLabel(e, UNEXPLORED ) for all v  G.vertices() if getLabel(v)  UNEXPLORED BFS(G, v)

53 53 Example C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F

54 54 Example (cont.) CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

55 55 Example (cont.) CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

56 56 Properties Notation G s : connected component of s Property 1 BFS(G, s) visits all the vertices and edges of G s Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree T s of G s Property 3 For each vertex v in L i  The path of T s from s to v has i edges  Every path from s to v in G s has at least i edges CB A E D L0L0 L1L1 F L2L2 CB A E D F

57 57 Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice  once as UNEXPLORED  once as VISITED Each edge is labeled twice  once as UNEXPLORED  once as DISCOVERY or CROSS Each vertex is inserted once into a sequence L i Method incidentEdges is called once for each vertex BFS runs in O(n  m) time provided the graph is represented by the adjacency list structure  Recall that  v deg(v)  2m

58 58 Applications Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n  m) time  Compute the connected components of G  Compute a spanning forest of G  Find a simple cycle in G, or report that G is a forest  Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists

59 59 DFS vs. BFS CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

60 60 DFS vs. BFS (cont.) Back edge (v,w)  w is an ancestor of v in the tree of discovery edges Cross edge (v,w)  w is in the same level as v or in the next level in the tree of discovery edges CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS

61 61 Applications Space complexity of DFS is much lower than BFS (breadth-first search). It also lends itself much better to heuristic methods of choosing a likely-looking branch. Time complexity of both algorithms are proportional to the number of vertices plus the number of edges in the graphs they traverse (O(|V| + |E|)).

62 62 ITS033 Topic 01-Problems & Algorithmic Problem Solving Topic 01 - Problems & Algorithmic Problem Solving Topic 02 – Algorithm Representation & Efficiency Analysis Topic 03 - State Space of a problem Topic 04 - Brute Force Algorithm Topic 05 - Divide and Conquer Topic 06-Decrease and Conquer Topic 06 - Decrease and Conquer Topic 07 - Dynamics Programming Topic 08 - Transform and Conquer Topic 09 - Graph Algorithms Topic 10 - Minimum Spanning Tree Topic 11 - Shortest Path Problem Topic 12 - Coping with the Limitations of Algorithms Power http://www.siit.tu.ac.th/bunyarit/its033.php and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7 Midterm

63 63 End of Chapter 9 Thank you!


Download ppt "1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology."

Similar presentations


Ads by Google