cse 780 design and analysis of algorithms n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 780: Design and Analysis of Algorithms PowerPoint Presentation
Download Presentation
CSE 780: Design and Analysis of Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 28

CSE 780: Design and Analysis of Algorithms - PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on

CSE 780: Design and Analysis of Algorithms. Lecture 14: Directed Graph BFS DFS Topological sort. a. b. c. d. e. f. Un-directed graph Directed graph Each edge is directed from u to v. a. b. c. d. e. f. Vertex Degree. indeg(v) = # edges of the form

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

PowerPoint Slideshow about 'CSE 780: Design and Analysis of Algorithms' - howell


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
cse 780 design and analysis of algorithms

CSE 780: Design and Analysis of Algorithms

Lecture 14:

Directed Graph

BFS

DFS

Topological sort

CSE 2331/5331

slide2

a

b

c

d

e

f

  • Un-directed graph
  • Directed graph
    • Each edge is directed from u to v

CSE 2331/5331

vertex degree

a

b

c

d

e

f

Vertex Degree
  • indeg(v) = # edges of the form
  • outdeg(v) = # edges of the form
  • Lemma:
  • Lemma:

CSE 2331/5331

representations of graphs
Representations of Graphs
  • Adjacency lists
    • Each vertex u has a list, recording its neighbors
      • i.e., all v’s such that (u, v)  E
    • An array of V lists
      • V[i].degree = size of adj list for node
      • V[i].AdjList= adjacency list for node

CSE 2331/5331

adjacency lists
Adjacency Lists
  • For vertex v  V, its adjacency list
    • has size: outdeg(v)
    • decide whether (v, u)  E or not in time O(outdeg(v))
  • Size of data structure (space complexity):
    • O(V+E)

CSE 2331/5331

adjacency matrix
Adjacency Matrix
  • matrix
    • if is an edge
    • Otherwise,

CSE 2331/5331

adjacency matrix1
Adjacency Matrix
  • Size of data structure:
    • O ( V  V)
  • Time to determine if (v, u)  E :
    • O(1)
  • Though larger, it is simpler compared to adjacency list.

CSE 2331/5331

sample graph algorithm
Sample Graph Algorithm
  • Input: Directed graph G represented by adjacency list

Running time:

O(V + E)

CSE 2331/5331

connectivity
Connectivity
  • A path in a graph is a sequence of vertices such that there is an edge between any two adjacent vertices in the sequence
  • Given two vertices we say that wis reachable from u if there is a path in G from u to w.
    • Note: w is reachable from uDOES NOT necessarily mean that u is reachable from w.

CSE 2331/5331

reachability test
Reachability Test
  • How many (or which) vertices are reachable from a source node, say ?

CSE 2331/5331

bfs and dfs
BFS and DFS
  • The algorithms for BFS and DFS remain the same
    • Each edge is now understood as a directed edge
  • BFS(V,E, s) :
    • visits all nodes reachable from s in non-decreasing order

CSE 2331/5331

slide12
BFS
  • Starting from source node s,
    • Spread a wavefront to visit other nodes
    • First visit all nodes one edge away from s
    • Then all nodes two edges away from s

CSE 2331/5331

pseudo code
Pseudo-code

Use adjacency list

representation

Time complexity:

O(V+E)

CSE 2331/5331

number of reachable nodes
Number of Reachable Nodes

Compute # nodes

reachable from

Time complexity:

O(V+E)

CSE 2331/5331

dfs depth first search
DFS: Depth-First Search
  • Similarly, DFS remains the same
    • Each edge is now a directed edge
  • If we start with all nodes unvisited,
    • Then DFS(G, visits all nodes reachable to node
  • BFS from previous NumReachable() procedure can be replaced with DFS.

CSE 2331/5331

more example
More Example
  • Is reachable from ?
  • Is reachable from ?

CSE 2331/5331

slide17

DFS above can be replaced with BFS

DFS(G, k);

CSE 2331/5331

topological sort
Topological Sort

CSE 2331/5331

directed acyclic graph
Directed Acyclic Graph
  • A directed cycle is a sequence such that there is a directed edge between any two consecutive nodes.
  • DAG: directed acyclic graph
    • Is a directed graph with no directed cycles.

CSE 2331/5331

topological sort1

undershorts

socks

watch

pants

shoes

shirt

belt

tie

jacket

Topological Sort
  • A topological sort of a DAG G = (V, E)
    • A linear ordering A of all vertices from V
    • If edge (u,v)  E => A[u] < A[v]

CSE 2331/5331

another example
Another Example

Why requires DAG?

Is the sorting order

unique?

CSE 2331/5331

slide22

A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG).

CSE 2331/5331

question
Question
  • How to topologically sort a given DAG?
  • Intuition:
    • Which node can be the first node in the topological sort order?
    • A node with in-degree 0 !
    • After we remove this, the process can be repeated.

CSE 2331/5331

example

undershorts

socks

watch

pants

shoes

shirt

belt

tie

jacket

Example

CSE 2331/5331

slide27

Time complexity

    • O(V+E)
  • Correctness:
    • What if the algorithm terminates before we finish visiting all nodes?
    • Procedure TopologicalSort(G) outputs a sorted list of all nodes if and only if the input graph G is a DAG
      • If G is not DAG, the algorithm outputs only a partial list of vertices.

CSE 2331/5331

remarks
Remarks
  • Other topological sort algorithm by using properties of DFS

CSE 2331/5331