meljun_cortes_jedi slides data structures chapter06 graphs
TRANSCRIPT
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
1/35
1Data Structures Graphs
6 Graphs
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
2/35
2Data Structures Graphs
ObjectivesAt the end of the lesson, the student should be able to: Explain the basic concepts and definitions on graphs Discuss the methods of graph reprentation : adjacency
matrix and adjacency list Traverse graphs using the algorithms depth-first search
and breadth-first search Get the minimum cost spanning tree for undirected
graphs using Prim's algorithm and Kruskal's algorithms Solve the single-source shortest path problem using
Dijkstra's algorithm Solve the all-pairs shortest path problem using Floyd's
algorithm
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
3/35
3Data Structures Graphs
Definition and Related
Concepts A graph , G = ( V, E ), consists of a finite, non-empty set of vertices, V, and a set of edges, E
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
4/35
4Data Structures Graphs
Definition and Related
Concepts undirected graph - graph in which the pair of vertices
representing an edge is unordered
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
5/35
5Data Structures Graphs
Definition and Related
Concepts Two vertices i and j are adjacent if (i, j) is an edge in E. Theedge (i, j) is said to be incident on vertices i and j
complete undirected graph - graph in which an edgeconnects every pair of vertices
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
6/35
6Data Structures Graphs
Definition and Related
Concepts directed graph (or digraph ) - graph in which the pair of vertices representing an edge is ordered
complete directed graph - graph in which every pair of vertices i and j are connected by two edges and
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
7/35
7Data Structures Graphs
Definition and Related
Concepts subgraph of a graph G = (V,E) is a graph G = (V,E) suchthat V V and E E
A path from vertex u to vertex w in a graph G = (V,E) is a
sequence of vertices v o, v1, v2, ..., v m-1 , vm where v o
uand v m w, such that (v 0,v1),(v1,v2),...,(v m-1 , vm)[ , , ..., ] are edges in E. Thelength of a path refers to the number of edges in it
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
8/35
8Data Structures Graphs
Definition and Related
Concepts simple path - path in which all vertices are distinct, except
possibly the first and the last simple cycle - simple path that has the same start and end
vertex Two vertices i and j are connected if there is a path from
vertex i to vertex j. If for every pair of distinct vertices i and jthere is a directed path to and from both vertices, it is said to
be strongly connected weighted graph - graph with weights or costs assigned to
its edges
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
9/35
9Data Structures Graphs
Definition and Related
Concepts spanning tree - subgraph that connects all the vertices of agraph. If the spanning tree is weighted, its cost is the sum of the weights of the its branches. A minimum cost spanningtree is a spanning tree that has the minimum cost
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
10/35
10Data Structures Graphs
Graph Representations Adjacency matrix for directed graphs
use a two dimensional matrix, say A, with dimension n x n , where nis the number of vertices where
A(i,j) =1 if the edge exists, 1 i, j n= 0 otherwise
cost-adjacency matrix can be used if the graph is weighted. A(i, j)is set to contain the cost of edge , but if there is no edge in the graph, A(i, j) is set to a very large value
outdegree of vertex i - number of arrows emanating from it indegree of vertex j - number of arrows pointing to it
O(n 2 ) space requirement O(1) time in determining if there is an edge
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
11/35
11Data Structures Graphs
Graph Representations Adjacency matrix for directed graphs
1 2 3 4 5
1 0 1 9
2 0 2 5 10
3 0 3
4 4 0 8
5 6 0
Representation for Directed Graph
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
12/35
12Data Structures Graphs
Graph Representations Adjacency list for directed graphs
use a list such that for any vertex i in G, LIST(i) points to the list of verticesadjacent from i
LIST INFO COST NEXT
1 4 9 2 1
2 5 10 4 5 3 2
3 5 3
4 5 8 3 4
5 1 6
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
13/35
13Data Structures Graphs
Graph Representations Adjacency matrix for undirected graphs
similar to adjacency matrix for digraphs except that it is now symmetric It is either the lower or the upper diagonal elements are required to
represent the graph. The other part could be regarded as dont cares(*)
1 2 3 4 5
1 0 * * * *
2 0 0 * * *
3 0 1 0 * *
4 1 0 1 0 *
5 1 1 0 0 0
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
14/35
14Data Structures Graphs
Graph Representations Adjacency list for undirected graphs
similar to adjacency list for directed graph except that there are two entriesin the list for an edge (i, j)
LIST INFO NEXT
1 5 4
2 5 3
3 4 2
4 3 1
5 2 1
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
15/35
15Data Structures Graphs
Graph Traversals Depth First Search (DFS) - traverse as deeply as possible
Given a graph with vertices marked unvisited , the traversal isperformed as follows:
1) Select a start, unvisited vertex. If no vertex can be found, DFSterminates2) Mark start vertex visited3) Process adjacent vertex:
a) Select an unvisited vertex, say u, adjacent to (or from) thestart vertex
b) Mark the adjacent vertex visitedc) Initiate a DFS with u as start vertex. If no such vertex canbe found, go to step (1)
4) If more unvisited, adjacent vertices are encountered, go tostep (c)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
16/35
16Data Structures Graphs
Graph Traversals Depth First Search (DFS)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
17/35
17Data Structures Graphs
Graph Traversals Depth First Search (DFS)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
18/35
18Data Structures Graphs
Graph Traversals Breadth First Search (BFS) - traverse as broadly as
possible Given a graph with vertices marked unvisited , the traversal is
performed as follows:
1) Select an unvisited vertex, say vertex i, and mark itvisited. Then, search as broadly as possible from i byvisiting all the vertices adjacent to it
2) Repeat (1) until all the vertices in the graph are visited
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
19/35
19Data Structures Graphs
Graph Traversals Breadth First Search (BFS)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
20/35
20Data Structures Graphs
Graph Traversals Breadth First Search (BFS)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
21/35
21Data Structures Graphs
Graph Traversals Breadth First Search (BFS)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
22/35
22Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs * greedy approach - a sequence of opportunistic choices
succeeds in finding a global optimum
* MST Theorem:Let G = (V,E) be a connected, weighted, undirected graph. Let U be some proper subset of V and (u, v) be an edge of least cost such that u U and v (V U). There exists a minimum
cost spanning tree T such that (u, v) is an edge in T
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
23/35
23Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs Prims AlgorithmLet G = (V,E) be a connected, weighted, undirected graph. Let Udenote the set of vertices chosen and T denote the set of edgesalready taken in at any instance of the algorithm.
1) Choose initial vertex from V and place it in U.
2) From among the vertices in V - U choose that vertex, say v,which is connected to some vertex, say u , in U by an edge of least cost. Add vertex v to U and edge (u, v) to T.
3) Repeat (2) until U = V , in which case, T is a minimum costspanning tree for G.
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
24/35
24Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs Prims Algorithm
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
25/35
25Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs Prims Algorithm
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
26/35
26Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs Kruskal's AlgorithmLet G = (V, E) be a connected, weighted, undirected graph on nvertices. The minimum cost spanning tree, T, is built edge by edge,
with the edges considered in non-decreasing order of their cost1) Choose the edge with the least cost as the initial edge.
2) The edge of least cost among the remaining edges in E isconsidered for inclusion in T. If cycle will be created, the edge inT is rejected.
sorting the edges in non-decreasing order is a major factor in thecomputational cost
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
27/35
27Data Structures Graphs
Minimum Cost Spanning
Tree for Undirected Graphs Kruskal's Algorithm
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
28/35
28Data Structures Graphs
Shortest Path Problems for
Directed Graphs Single Source Shortest Paths (SSSP) Problem - determinesthe cost of the shortest path from a source vertex u to adestination vertex v, where u and v are elements of V
All-Pairs Shortest Paths (APSP) Problem - determines thecost of the shortest path from each vertex to every other vertex in V
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
29/35
29Data Structures Graphs
Shortest Path Problems for
Directed Graphs Dijkstra's Algorithm for the SSSP Problem greedy approach each vertex is assigned a class and a value
Class 1 vertex - vertex whose shortest distance from the sourcevertex, say k, has already been found; value is equal to itsdistance from vertex k along the shortest path
Class 2 vertex - vertex whose shortest distance from k has yetto be found; value is its shortest distance from vertex k foundthus far.
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
30/35
30Data Structures Graphs
Shortest Path Problems for
Directed Graphs Dijkstra's Algorithm for the SSSP Problem Let: u - source vertex and vertex, v - destination vertex, pivot - vertex
that is most recently considered to be a part of the path, path of avertex - direct source in the shortest path
1) Place vertex u in class 1 and all other vertices in class 2 .2) Set the value of vertex u to zero and the value of all other vertices toinfinity.3) Do the following until vertex v is placed in class 1 :
a) Define the pivot vertex as the vertex most recently placed in c lass 1 .b) Adjust all class 2 nodes in the following way:
i) If a vertex is not connected to the pivot vertex, its value remains thesame.ii) If a vertex is connected to the pivot vertex, replace its value by theminimum of its current value or the value of the pivot vertex plus thedistance from the pivot vertex to the vertex in class 2 . Set its path to
pivot .c) Choose a class 2 vertex with minimal value and place it in class 1 .
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
31/35
31Data Structures Graphs
Shortest Path Problems for
Directed Graphs Dijkstra's Algorithm for the SSSP Problem
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
32/35
32Data Structures Graphs
Shortest Path Problems for
Directed Graphs Floyd's Algorithm for the APSP Problem
more elegant approach than using Dijkstra's algorithm with everypair of sources and destinations
uses the cost adjacency matrix representation of a graph an n x n matrix, PATH , is needed to contain the immediate vertices
along the shortest path:PATH (i,j) = 0 initially indicates that the shortest path between
i and j is the edge (i,j) if it exists= k if including k in the path from i to j at the k th
iteration yields a shorter path
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
33/35
33Data Structures Graphs
Shortest Path Problems for
Directed Graphs Floyd's Algorithm for the APSP Problem1) Initialize A to be equal to COST:
A(i, j) = COST(i, j), 1 i, j n
2) If the cost of passing through the intermediate vertex k fromvertex i to vertex j will cost less than direct access from i to j, replaceA(i,j) with this cost and update PATH(i,j) as well:
For k = 1, 2, 3, ..., n :
a) A(i, j) = minimum [ A k-1 (i, j), A k-1 (i, k) + A k-1 (k, j) ] , 1 i,j n
b) If ( A(i, j) == Ak-1
(i, k) + Ak-1
(k, j) ) set PATH(i, j) = k
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
34/35
34Data Structures Graphs
Shortest Path Problems for
Directed Graphs Floyd's Algorithm for the APSP Problem
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter06 Graphs
35/35
35Data Structures Graphs
Summary A graph G= (V,E) consists of a finite, non-empty set of vertices
and a set of edges Adjacency matrix representation of graphs requires O(n 2) space.
Telling if there is an edge takes O(1) time
In adjacency list representation of graphs, a list is maintainedsuch that for any vertex i in G, LIST(i) points to the list of verticesadjacent from i
DFS traverses the graph as deeply as possible while BFStraverses the graph as broadly as possible
The minimun cost spanning tree problem can be solved usingPrim's algorithm or Kruskal's algorithm Dijkstra's algorithm is for the SSSP problem while Floyd's
algorithm is for the APSP problem