Chapter 16
1 / 52

Graphs - PowerPoint PPT Presentation

  • Updated On :

Chapter 16. Graphs. Outline. Graph Categories Digraph Connectedness of Digraph Adjacency Matrix, Set vertexInfo Object Breadth-First Search Algorithm Depth first Search Algorithm. Strong Components Graph G and Its Transpose G T Shortest-Path Dijkstra Minimum-Path Algorithm

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Graphs' - bert

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 16 l.jpg

Chapter 16


Slide2 l.jpg


  • Graph Categories

  • Digraph

  • Connectedness of Digraph

  • Adjacency Matrix, Set

  • vertexInfo Object

  • Breadth-First Search Algorithm

  • Depth first Search Algorithm

  • Strong Components

  • Graph G and Its Transpose GT

  • Shortest-Path

  • Dijkstra Minimum-Path Algorithm

  • Minimum Spanning Tree

  • Prim’s algorithm

  • Kruskal’s algorithm

Graph terminology l.jpg
Graph Terminology

  • A graph G=(V, E) consists of a set V of vertices and a set E of edges that connect pair of vertices.

    • V={v1, v2, …, vn}

    • E={e1, e2, …, em}

    • An edge e E is a pair (vi, vj)

  • A subgraph Gs=(Vs, Es) is a subset of the vertices and edges, where Vs V, Es E

  • Two vertices, vi, vj are adjacent if and only if there is an edge e=(vi, vj) E

  • A path in a graph is a sequence of vertices v0, v1, v2, …, vk such that (vi, vI+1) E

    • Simple Path: each vertex occur only once

    • Cycle: there is a vertex appearing more than once on a path

Graph terminology4 l.jpg

  • A graph is connected if each pair of vertices have a path between them

  • A complete graph is a connected graph in which each pair of vertices are linked by an edge

Directed graphs l.jpg
Directed Graphs

  • Graph with ordered edges are called directedgraphs or digraphs, otherwise it is undirected.

  • The number of edges that emanate from a vertex v is called the out-degree of v

  • The number of edges that terminate on vertex is called the in-degree of v

Directed acyclic graph dag l.jpg
Directed Acyclic Graph (DAG)

  • A directed graph that has no cycle is called a directed acyclic graph (DAG)

    • Directed path

    • Directed cycle: a directed path of length 2 or more that connects a vertex to itself

  • A weighted digraph is a directed graph that associates values with the edges.

    • A weight edge e=(vi, vj, wt)

Connectedness of digraph l.jpg
Connectedness of Digraph

  • Strongly connected if for each pair of vertices vi and vj, there is a path P(vi, vj)

  • Weakly connected if for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vj,vi).

The graph class l.jpg
The Graph Class

  • Access the properties of a graph

  • Add or delete vertices and edges

  • Update the weight of an edge

  • Identify the list of adjacent vertices

Representation of graphs l.jpg
Representation of Graphs

  • Adjacency matrix

  • Adjacent set

Adjacency matrix l.jpg
Adjacency Matrix

  • An n by n matrix, called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (vi, vj). Its value is the weight of the edge, or -1 if the edge does not exist.

Adjacency set l.jpg
Adjacency Set

  • For each vertex, an element in the adjacent set is a pair consisting of the adjacent vertex and the weight of the edge.

Vertexinfo object l.jpg
vertexInfo Object

  • A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

Vertex map and vector vinfo l.jpg
Vertex Map and Vector vInfo

  • To store the vertices in a graph, we provide a map<T,int> container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector

Graph traversal algorithms l.jpg
Graph Traversal Algorithms

  • Breadth-First Visit(Search): visits vertices in the order of their path length from a starting vertex. (generalizes the level-order scan in a binary tree)

  • Depth-First Visit(Search): traverses vertices of a graph by making a series of recursive function calls that follow paths through the graph. (emulate the postorder scan in a binary tree)


DFS (reverse order of processing): A,C,B,D,F,G,E

Implementation of bfs algorithm l.jpg
Implementation of BFS algorithm

  • uses a queue to store the vertices awaiting a visit temporally.

  • At each iterative step, the algorithm deletes a vertex from the queue, mark it as visited, and then inserts it into visitSet, the set of visited vertices.

  • The step concludes by placing all unvisited neighbors of the vertex in the queue.

  • Each vertex is associate a color from WHITE, GRAY, BLACK.

    • Unvisited: (Initially) WHITE

    • In the process of being searched: (enter into queue) GRAY

    • Visited: (remove from queue) BLACK

  • Time complexity: O(|V|)+O(|E|)

Depth first search algorithm l.jpg
Depth-First Search Algorithm

Discovery order: A, B, D, E, F, G, C

Finishing order: E, G, F, D, B, C, A

Depth first search cont l.jpg




Depth-First Search… (Cont.)





Discovery order & finishing order?

Implementing the dfs l.jpg
Implementing the DFS

  • dfsVisit()

    • Includes four arguments: a graph, a WHITE starting vertex, the list of visited vertices in reverse order of finishing times, and Boolean variable for checking cycle

    • Search only vertices that are reachable from the starting vertex

  • dfs()

    • Takes two arguments: a graph and a list

    • Repeatedly call dfsVisit()

  • Time complexity: O(|V|)+O(|E|)

dfsList: descending/reverse order of their visit (finish-time) order

Graph traversal applications l.jpg
Graph Traversal Applications

  • Acyclic graphs

  • Topological sort

  • Strongly connected component

Acyclic graphs l.jpg
Acyclic graphs

  • A graph is acyclic if it contains no cycles

  • Function acyclic() determine if the graph is acyclic

    • Back edge (v,w): current vertex v and a neighbor vertex w with color gray

Topological sort l.jpg
Topological sort

  • Topological order: if P(v,w) is a path from v to w, then v must occur before w in the list.

  • If graph is acyclic, defList produce a topological sort of the vertices

  • Implementation, performance, & applications

Strong components l.jpg








Strong Components

  • A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.


A, B, C

D, F, G


Graph g and its transpose g t l.jpg
Graph G and Its Transpose GT

  • The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.

Finding strong components l.jpg
Finding Strong Components

  • Algorithm

    • Step 1. Execute dfs() for graph

    • Step 2. Generate the transpose graph, GT

    • Step 3. Execute a series of dfsVisit( ) calls for vertices using the order of the elements in dfsList obtained in step 1 as the starting vertices.

Finding strong components29 l.jpg
Finding Strong Components

  • Example, Verification, Implementation, performance

Graph minimization algorithms l.jpg
Graph-Minimization Algorithms

  • Shortest-Path Algorithm

  • Dijkstra’s Minimum-Path Algorithm

  • Minimum-Spanning Tree Algorithms

Shortest path algorithm l.jpg
Shortest-Path algorithm

  • Find a shortest path (minimum number of edges) from a given vertex vs to every other vertex in a directed graph

  • The shortest-path algorithm includes a queue that indirectly stores the vertices, using the corresponding vInfo index.

  • Each iterative step removes a vertex from the queue and searches its adjacency set to locate all of the unvisited neighbors and add them to the queue.

Shortest path example l.jpg
Shortest-Path Example

  • Example: Find the shortest path from F to C.

Shortest path algorithm33 l.jpg
Shortest-Path algorithm

  • Implementation

  • Performance

    • Similar to BFS search, O(|V|+|E|)

Dijkstra minimum path algorithm l.jpg
Dijkstra Minimum-Path Algorithm

  • Find the minimum weight (minimum sum of edge weight) from a given vertex to every other vertex in a weighted directed graph

  • Use a minimum priority queue to store the vertices, using minInfo class, which contains a vInfo index and the pathWeight value

  • At each iterative step, the priority queque identifies a new vertex whose pathWeight value is the minimum path weight from the starting vertex

Dijkstra minimum path algorithm from a to d example l.jpg




priority queue

Dijkstra Minimum-Path Algorithm From A to D Example

Dijkstra minimum path algorithm36 l.jpg
Dijkstra Minimum-Path Algorithm

  • The Dijkastra algorithm is a greedy algorithm

    • at each step, make the best choice available.

    • Usually, greedy algorithms produce locally optimum results, but not globally optimal

    • Dijkstra algorithm produces a globally optimum result

  • Implementation

  • Running-Time Analysis

    • O(|V|+|E|log|E|)

Minimum spanning tree algorithms l.jpg
Minimum Spanning Tree Algorithms

  • Minimum Spanning Tree (For Undirected Graph)

    • Tree: a connected graph with no cycles.

    • Spanning Tree: a tree which contains all vertices in G.

      • Note: Connected graph with n vertices and exactly n – 1 edges is Spanning Tree.

    • Minimum Spanning Tree: a spanning Tree with minimum total weight

Prim s algorithm l.jpg
Prim’s Algorithm

  • Basic idea: Start from vertex 1 and let T  Ø (T will contain all edges in the S.T.); the next edge to be included in T is the minimum cost edge(u, v), s.t. u is in the tree and v is not.

Implementation of prim s algorithm l.jpg
Implementation of Prim’s algorithm:

  • Using a priority queque of miniInfo objects to store information

    • Color: White not in tree, Black in tree

    • Data Value: the weight of the minimum edge that would connect the vertex to an existing vertex in tree

    • parent

  • Each iterative steps similar to Dijkstra algorithm

Prim s minimum spanning tree algorithm example l.jpg











Prim’s Minimum Spanning-Tree Algorithm example:

Prim s minimum spanning tree algorithm l.jpg
Prim’s Minimum Spanning Tree Algorithm

  • Implementation

  • Running-Time Analysis

    • O(|V|+|E|log|E|)

Slide44 l.jpg

















  • Kruskal’s Algorithm

  • Basic idea: Don’t care if T is a tree or not in the intermediate stage, as long as the including of a new edge will not create a cycle, we include the minimum cost edge


Step 1: Sort all of edges

(1,2) 10 √

(3,6) 15 √

(4,6) 20 √

(2,6) 25 √

(1,4) 30 × reject create cycle

(3,5) 35 √

Slide45 l.jpg























Step 2: T

Slide46 l.jpg

  • How to check: adding an edge will create a cycle or not?

  • If Maintain a set for each group

  • (initially each node represents a set)

  • Ex: set1set2set3

  •  new edge

  • from different groups  no cycle created

  • Data structure to store sets so that:

    • The group number can be easily found, and

    • Two sets can be easily merged









Slide47 l.jpg

Kruskal’s algorithm

While (T contains fewer than n-1 edges) and (E   ) do


Choose an edge (v,w) from E of lowest cost;

Delete (v,w) from E;

If (v,w) does not create a cycle in T

then add (v,w) to T

else discard (v,w);


Time complexity: O(|V|+|E|log|E|)

Summary slide 1 l.jpg
Summary Slide 1

§- Undirected and Directed Graph (digraph)

- Both types of graphs can be either weighted or nonweighted.


Summary slide 2 l.jpg
Summary Slide 2

§- Breadth-First, bfs()

- locates all vertices reachable from a starting vertex

- can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.


Summary slide 3 l.jpg
Summary Slide 3

§- Depth-First search, dfs()

- produces a list of all graph vertices in the reverse order of their finishing times.

- supported by a recursive depth-first visit function, dfsVisit()

- an algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG)

- forms the basis for an efficient algorithm that finds the strong components of a graph


Summary slide 4 l.jpg
Summary Slide 4

§-Dijkstra's algorithm

- if weights, uses a priority queue to determine a path from a starting to an ending vertex, of minimum weight

- This idea can be extended to Prim's algorithm, which computes the minimum spanning tree in an undirected, connected graph.


Summary slide 5 l.jpg
Summary Slide 5

§-Minimum Spanning Tree algorithm

- Prim’s algorithm

- Kruskal’s Algorithm