Docsity

Big O Notation and Graphs, Exams of Advanced Algorithms

Partial preview of the text

Download Big O Notation and Graphs and more Exams Advanced Algorithms in PDF only on Docsity!

Big O Notation and Graphs

CMSC 132 - Week 9

Graphs API

โ— 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 can be objects that store some information. โ— Example: โ—‹ A vertex represents an airport and stores the 3-letter airport code โ—‹ An edge represents a flight route between 2 airports, and stores the route mileage Figure from Dr. Noha Adlyโ€™s lectures, Alexandria University

Terminology

โ— End vertices or endpoints of an edge ( u, v ) โ— Degree of a vertex in an undirected graph (in-degree and out-degree for directed graphs) โ— Parallel edges: h and i are parallel edges โ— Self-loops: j is a self-loop Figure from Dr. Noha Adlyโ€™s lectures, Alexandria University

Graph Representation

Adjacency List Adjacency Matrix List of Edges (A,B) (A,C) (A,D) (B,E) (B,F) (C,F)

Depth-first Search

Depth-first Search Recursive

Depth-first Search

Figure from Dr. Noha Adlyโ€™s lectures, Alexandria University

Depth-first Nonrecursive Pseudocode

Example

Output: A D C F B E

Clicker Quiz

  1. What is the traversal order of this graph using DFS-recursive version? The start point is 6. A.6, 4, 3, 2, 1, 5 B.6, 4, 5, 2, 3, 1 C.6, 4, 5, 1, 2, 3 D.6, 4, 3, 2, 5, 1

Clicker Quiz

  1. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 6. A.6, 4, 3, 2, 5, 1 B.6, 4, 5, 1, 2, 3 C.6, 4, 5, 2, 1, 3 D.6, 4, 3, 2, 1, 5

Clicker Quiz

  1. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 6. A.6, 4, 3, 2, 5, 1 B.6, 4, 5, 1, 2, 3 C.6, 4, 5, 2, 1, 3 D.6, 4, 3, 2, 1, 5

Clicker Quiz

  1. What is the traversal order of this graph using DFS-recursive version? The start point is 1. A.1, 3, 5, 4, 2 B.1, 2, 4, 5, 3 C.1, 2, 5, 4, 3 D.1, 2, 3, 4, 5

Clicker Quiz

  1. What is the traversal order of this graph using DFS-nonrecursive version? The start point is 1. A.1, 2, 3, 4, 5 B.1, 2, 5, 4, 3 C.1, 2, 4, 5, 3 D.1, 4, 3, 5, 2