Brute Force: Selection Sort

Size: px
Start display at page:

Download "Brute Force: Selection Sort"

Transcription

1 Brute Force: Intro Brute force means straightforward approach Usually based directly on problem s specs Force refers to computational power Usually not as efficient as elegant solutions Advantages: Applicable to wide variety of problems May produce reasonable algorithm of practical value with no limitation on instance size Expense of generating elegant solution may not be justified Handy for small-size problems Are the basis by which other algorithms can be compared 1

2 Brute Force: Selection Sort Algorithm: SS(A[0..n - 1]) { \\Input: Array A[0..n - 1] \\Output: Sorted array for ( i <- 0 to n - 2) { min <- i for ( j <- i + 1 to n - 1) if (A[j] < A[min] min <- j swap A[i], A[min] Analysis: Size = number of elements (n) Basic op is comparison (A[j] < A[min]) Work depends only on n C(n) = n 2 i=0 n 1 j=i+1 1 = n 2 i=0 [(n 1) (i + 1) + 1] = n 2 i=0 (n 1 i) = (n 1)n 2 SS Θ(n 2 ) Number of swaps Θ(n 2 ) 2

3 Brute Force: Bubble Sort Algorithm: BS(A[0..n - 1]) { \\Input: Array A[0..n - 1] \\Output: Sorted array for ( i <- 0 to n - 2) { for ( j <- 0 to n i) if (A[j + 1] < A[j] swap A[i], A[j + 1] Analysis: Size = number of elements (n) Basic op is comparison (A[j + 1] < A[j]) Work depends only on n C(n) = n 2 i=0 n 2 i j=0 1 = n 2 i=0 [(n 2 i) 0 + 1] = n 2 i=0 (n 1 i) = (n 1)n 2 BS Θ(n 2 ) Number of swaps depends on instance: worst case Θ(n 2 )) Can improve by keeping track of swaps 3

4 Brute Force: Sequential Search Algorithm: SqS(A[0..n - 1], k) { \\Input: Array A[0..n] on n elements in slots 0.. n - 1, search key k \\Output: Index i to k (or -1) A[n] <- k i <- 0 while( A[i]!= k) i++ if (i < n) return i else return -1 Analysis: See chapter 2 4

5 Brute Force: String Matching Problem: Given text string of n characters and pattern string of m characters, find i - the leftmost index of pattern in text Algorithm: BFSM(T[0..n - 1], P[0..m - 1]) { \\Input: String T[0..n - 1] and pattern P[0..m - 1] \\Output: Index i to P s position in T (or -1) for ( i <- 0 to n - m) { j <- 0 while ( j < m AND P[j] == T[i + j]) j++ if (j == m) return i return -1 Analysis: Size = number of elements (n) Basic op is comparison (P [j] = T [i + j]) Work depends only on instance Usually, shift occurs after 1 comparison Worst case makes m comparisons per index: Θ(mn) Average case: Θ(n + m) = Θ(n) 5

6 Brute Force: Closest Pair Problem Problem: Given a set of n planar points, find 2 such that the distance between them is the shortest between any pair of points in the set Algorithm: BFCP(P) { \\Input: Array P[1..n] of points \\Output: Indices index1, index2 to closest points dmin <- infinity for ( i <- 1 to n - 1) for ( j <- i + 1 to n) { d <- sqrt((p[i].x - P[j].x)^2 + (P[i].y - P[j].y)^2) // More efficiently: d <- ((P[i].x - P[j].x)^2 + (P[i].y - P[j].y)^2) if (d < dmin) { dmin <- d index1 <- i index2 <- j return index1, index2 Analysis: Size = number of points (n) Basic op is calculating distance Work depends only on n C(n) = n 1 i=1 nj=i+1 2 = 2 n 1 i=1 (n i) = 2[(n 1) + (n 2) ] = n(n 1) BF CP Θ(n 2 ) 6

7 Brute Force: Convex Hull Problem Basic concepts: Convex set of coplanar points is set such that a line between any 2 of the points falls entirely within the set Convex hull of n coplanar points is the smallest convex polygon that contains all n points Extreme point is a vertex of a convex hull Theorem: The convex hull of set S of n > 2 points (not all colinear) is a convex polygon with vertices at some points of S The convex hull problem: Given n points, construct a convex hull for the points; i.e., find the extreme points of the encompassing polygon The basic approach: 2 points of set S form an edge of the convex hull of S if all of the remaining points fall on the same side of the line connecting the 2 points Given points p 1 (x 1, y 1 ), p 2 (x 2, y 2 ), the line through them is defined by ax + by = c, where a = y 2 y 1, b = x 1 x 2, c = x 1 y 2 y 1 x 2 For an arbitrary point p i (x i, y i ) p i is on the line if ax i + by i = c p i is on one side of the line or the other if ax i + by i < c or ax i + by i > c To determine extreme points: For each n(n 1) 2 pairs of points, determine the sign of ax + by c for the remaining n 2 points ch(n) Θ(n 3 ) 7

8 Brute Force: Exhaustive Search Brute force approach to combinatorial problems: 1. Generate each element of domain 2. Select those that meet criteria 3. Select solution This section ignores generation of combinatorial objects 8

9 Brute Force: Traveling Salesman Problem Given a set of cities and roads between them, determine the cheapest route that visits each city exactly once. Represent as a weighted graph Vertices represent cities Edges represent paths Edge weights represent costs Problem reduces to finding shortest Hamiltonian circuit of graph Brute force approach: For each node, find all possible node sequences that begin and end at the node. This consists of the initial node and all permutations of the remaining n - 1 nodes Only 1/2 of these need be considered Number of permutations = (n 1)! 2 9

10 Brute Force: Knapsack Problem Given a container of capacity x and n items of capacity c i and value v i, find the set of items of greatest value that can fit into the container. For example: item size value Brute force approach examines all possible subsets of the n items, selecting the one with greatest value Number of subsets (power set) of n items is 2 n ks(n) Ω(2 n ) 10

11 Brute Force: Assignment Problem Given n people and n tasks, with each person assigned a rating for each task, find the best way of assigning the tasks to the people. For example: person tasks Most natural representation is a matrix Solution is an n-tuple, with 1 value from each column, no 2 of which come from the same row Select solution from permutations of 1..n (row numbers) ap(n) Ω(n!) 11

12 Basic approach: Decrease and Conquer: Depth-First Graph Traversal 1. From the root, follow a path to a leaf 2. When dead-end at a leaf, backtrack to nearest ancestor with unexplored branches and repeat Explores the tree branch-by-branch Algorithm: Alg DFT (G) { //Input: Graph G = {V, E //Output: Graph G with vertices numbered in order of visitation mark each vertex with 0 count <- 0 for (each vertex v in V) if (v marked 0) dft(v) Alg dft (v) { //Input: Vertex v, count //Output: Marked vertices, updated count count++ mark v with count for (each vertex w adjacent to v) if (w marked 0) dft(w) Iterative version uses a programmer-controlled stack, pushing vertices when first visited, popping when all children expanded Can create a depth-first forest during traversal: When encounter unvisited vertex, create edge from parent to vertex called tree edge When encounter visited vertex, create edge from it to parent called back edge The parent node is an ancestor of the vertex in the search forest Graph of search forest same as original with vertices partitioned into 2 sets (tree and back edges) 12

13 Decrease and Conquer: Depth-First Search (2) Analysis: Order of growth depends on data structure used to represent graph For adjacency matrix: Θ( V 2 ) For linked list: Θ( V + E ) Order in which nodes visited and exited are distinct and can be used in other algs DFT can test for connectivity by determining if any vertices remain unmarked after exit 1st vertex DFT can test for cycles by determining if any back edges exist 13

14 Basic approach: Decrease and Conquer: Breadth-First Traversal 1. From the root, visit each child 2. Repeat, visiting the immediate children of each of the above nodes;... Explores the graph level-by-level Algorithm: Alg BFT (G) { //Input: Graph G = {V, E //Output: Graph G with vertices numbered in order of visitation mark each vertex v in V with 0 count <- 0 for (each vertex v in V) if (v marked 0) bft(v) Alg bft (v) { //Input: Vertex v, count //Output: Marked vertices, updated count count++ mark v with count add v to queue while (queue NOT empty) { v <- front of queue for (each vertex w adjacent to v) if (w marked 0) { count++ mark w with count add w to queue Analysis: Order of growth is same as for depth-first Vertices exited in same order as first visited Can be used for connectivity and cycles just like depth-first Finds shortest path from root to given vertex Can create breadth-first forest in same manner as for depth-first traversal No back edges Previously marked vertices constitute cross edges Are links to siblings of either parent or vertex itself 14

15 Decrease and Conquer: DFT/BFT Comparison DFT BFT Data structure stack queue No. vertex orders 2 1 Edge types tree tree back cross Apps connectivity connectivity acyclicity acyclicity articulation points min-edge paths Efficiency: adj mat Θ( V 2 ) Θ( V 2 ) Efficiency: lnk lst Θ( V + E ) Θ( V + E ) 15

16 Decrease and Conquer: Digraphs Have direction associated with edge Represented same way as undirected graphs except Adjacency matrix usually not symmetric Linked list has just 1 node per edge DFT and BDT both apply DFT forest can have 4 edge types: 1. Tree edge 2. Back edge - can link to parent 3. Forward edge - links to descendent 4. Cross edge - None of the above Back edge indicates cycle Directed Acyclic Graph (DAG) is directed graph with no cycles 16

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach CSC 8301- Design and Analysis of Algorithms Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms Brute-Force Approach Brute force is a straightforward approach to solving a problem, usually

More information

Lecture 3. Brute Force

Lecture 3. Brute Force Lecture 3 Brute Force 1 Lecture Contents 1. Selection Sort and Bubble Sort 2. Sequential Search and Brute-Force String Matching 3. Closest-Pair and Convex-Hull Problems by Brute Force 4. Exhaustive Search

More information

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms Computer Science 385 Design and Analysis of Algorithms Siena College Spring 2019 Topic Notes: Brute-Force Algorithms Our first category of algorithms are called brute-force algorithms. Levitin defines

More information

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation) MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first

More information

CSCE 350: Chin-Tser Huang. University of South Carolina

CSCE 350: Chin-Tser Huang. University of South Carolina CSCE 350: Data Structures and Algorithms Chin-Tser Huang huangct@cse.sc.edu University of South Carolina Announcement Homework 2 will be returned on Thursday; solution will be available on class website

More information

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection

More information

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 5 Decrease-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance

More information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical Session No. 12 Graphs, BFS, DFS, Topological sort Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture VI: Chapter 5, part 2; Chapter 6, part 1 R. Paul Wiegand George Mason University, Department of Computer Science March 8, 2006 Outline 1 Topological

More information

CS 440 Theory of Algorithms /

CS 440 Theory of Algorithms / CS 440 Theory of Algorithms / CS 468 Algorithms in Bioinformaticsi Brute Force. Design and Analysis of Algorithms - Chapter 3 3-0 Brute Force A straightforward approach usually based on problem statement

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

Module 5 Graph Algorithms

Module 5 Graph Algorithms Module 5 Graph lgorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 97 E-mail: natarajan.meghanathan@jsums.edu 5. Graph Traversal lgorithms Depth First

More information

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted. CS6402 Design and Analysis of Algorithms _ Unit II 2.1 UNIT II BRUTE FORCE AND DIVIDE-AND-CONQUER 2.1 BRUTE FORCE Brute force is a straightforward approach to solving a problem, usually directly based

More information

Graph Algorithms. Definition

Graph Algorithms. Definition Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of

More information

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search CSC 8301 Design and Analysis of Algorithms: Exhaustive Search Professor Henry Carter Fall 2016 Recap Brute force is the use of iterative checking or solving a problem by its definition The straightforward

More information

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,

More information

Trees. Arash Rafiey. 20 October, 2015

Trees. Arash Rafiey. 20 October, 2015 20 October, 2015 Definition Let G = (V, E) be a loop-free undirected graph. G is called a tree if G is connected and contains no cycle. Definition Let G = (V, E) be a loop-free undirected graph. G is called

More information

Lecture 3: Graphs and flows

Lecture 3: Graphs and flows Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected

More information

LECTURE 11 TREE TRAVERSALS

LECTURE 11 TREE TRAVERSALS DATA STRUCTURES AND ALGORITHMS LECTURE 11 TREE TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD BACKGROUND All the objects stored in an array or linked list can be accessed sequentially

More information

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there

More information

Chapter 9. Priority Queue

Chapter 9. Priority Queue Chapter 9 Priority Queues, Heaps, Graphs Spring 2015 1 Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed 2Spring 2015 Priority Depends on the Application

More information

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset

More information

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Problem Score Maximum MC 34 (25/17) = 50 Total 100 Stony Brook University Midterm 2 CSE 373 Analysis of Algorithms November 22, 2016 Midterm Exam Name: ID #: Signature: Circle one: GRAD / UNDERGRAD INSTRUCTIONS: This is a closed book, closed mouth exam.

More information

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS 2 marks UNIT-I 1. Define Algorithm. An algorithm is a sequence of unambiguous instructions for solving a problem in a finite amount of time. 2.Write a short note

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of

More information

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville Computer Science and Software Engineering University of Wisconsin - Platteville 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 3.7-3.9,3.12, 4. Problem Solving as

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

CS61BL. Lecture 5: Graphs Sorting

CS61BL. Lecture 5: Graphs Sorting CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)

More information

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Practical Session No. 12 Graphs, BFS, DFS Topological Sort Practical Session No. 12 Graphs, BFS, DFS Topological Sort Graphs and BFS Graph G = (V, E) Graph Representations (VG ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in G,

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Introduction to Algorithms: Brute-Force Algorithms

Introduction to Algorithms: Brute-Force Algorithms Introduction to Algorithms: Brute-Force Algorithms Introduction to Algorithms Brute Force Powering a Number Selection Sort Exhaustive Search 0/1 Knapsack Problem Assignment Problem CS 421 - Analysis of

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA INTERNAL ASSESSMENT TEST 2 Date : 30/3/15 Max Marks : 50 Name of faculty : Sabeeha Sultana Subject & Code : ADA(13MCA41) Answer any five full question: 1.Illustrate Mergesort for the dataset 8,3,2,9,7,1,5,4.

More information

Graphs: basic concepts and algorithms

Graphs: basic concepts and algorithms : basic concepts and algorithms Topics covered by this lecture: - Reminder Trees Trees (in-order,post-order,pre-order) s (BFS, DFS) Denitions: Reminder Directed graph (digraph): G = (V, E), V - vertex

More information

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions. UNIT 3 BASIC TRAVERSAL AND SEARCH TECHNIQUES Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions. Search involves

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION 1. What is performance measurement? 2. What is an algorithm? 3. How the algorithm is good? 4. What are the

More information

Graph Algorithms Using Depth First Search

Graph Algorithms Using Depth First Search Graph Algorithms Using Depth First Search Analysis of Algorithms Week 8, Lecture 1 Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Graph Algorithms Using Depth

More information

Backtracking is a refinement of the brute force approach, which systematically searches for a

Backtracking is a refinement of the brute force approach, which systematically searches for a Backtracking Backtracking is a refinement of the brute force approach, which systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are

More information

Reference Sheet for CO142.2 Discrete Mathematics II

Reference Sheet for CO142.2 Discrete Mathematics II Reference Sheet for CO14. Discrete Mathematics II Spring 017 1 Graphs Defintions 1. Graph: set of N nodes and A arcs such that each a A is associated with an unordered pair of nodes.. Simple graph: no

More information

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs 5 3 2 4 1 0 2 Graphs A graph G = (V, E) Where V is a set of vertices E is a set of edges connecting vertex pairs Example: V = {0, 1, 2, 3, 4, 5} E = {(0, 1),

More information

Undirected Graphs. Hwansoo Han

Undirected Graphs. Hwansoo Han Undirected Graphs Hwansoo Han Definitions Undirected graph (simply graph) G = (V, E) V : set of vertexes (vertices, nodes, points) E : set of edges (lines) An edge is an unordered pair Edge (v, w) = (w,

More information

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]: CSE 101 Homework 1 Background (Order and Recurrence Relations), correctness proofs, time analysis, and speeding up algorithms with restructuring, preprocessing and data structures. Due Thursday, April

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 8: Graphs Jan Křetínský Winter 2017/18 Chapter 8: Graphs, Winter 2017/18 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set

More information

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Undirected graphs Concepts: connectivity, connected components paths (undirected) cycles Basic problems, given undirected graph G: is G connected how many

More information

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions: Direct Addressing - key is index into array => O(1) lookup Hash table: -hash function maps key to index in table -if universe of keys > # table entries then hash functions collision are guaranteed => need

More information

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016 CSCE 321/3201 Analysis and Design of Algorithms Prof. Amr Goneid Fall 2016 CSCE 321/3201 Analysis and Design of Algorithms Prof. Amr Goneid Course Resources Instructor: Prof. Amr Goneid E-mail: goneid@aucegypt.edu

More information

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I PART A(2MARKS) 1. What is an algorithm? 2. What is meant by open hashing? 3. Define Ω-notation 4.Define order of an algorithm. 5. Define O-notation

More information

( D. Θ n. ( ) f n ( ) D. Ο%

( D. Θ n. ( ) f n ( ) D. Ο% CSE 0 Name Test Spring 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to run the code below is in: for i=n; i>=; i--) for j=; j

More information

implementing the breadth-first search algorithm implementing the depth-first search algorithm

implementing the breadth-first search algorithm implementing the depth-first search algorithm Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm

More information

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

Assignment No 2 (Group B)

Assignment No 2 (Group B) Assignment No 2 (Group B) 1 Problem Statement : Concurrent Implementation of Travelling Salesman Problem. 2 Objective : To develop problem solving abilities using Mathematical Modeling. To apply algorithmic

More information

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations

More information

Graph Representation

Graph Representation Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III

More information

Trees Algorhyme by Radia Perlman

Trees Algorhyme by Radia Perlman Algorhyme by Radia Perlman I think that I shall never see A graph more lovely than a tree. A tree whose crucial property Is loop-free connectivity. A tree which must be sure to span. So packets can reach

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a

More information

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

Figure 1: A directed graph.

Figure 1: A directed graph. 1 Graphs A graph is a data structure that expresses relationships between objects. The objects are called nodes and the relationships are called edges. For example, social networks can be represented as

More information

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node. UNIT III : DYNAMIC STORAGE MANAGEMENT Trees: Binary tree, Terminology, Representation, Traversals, Applications. Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path.

More information

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302 Topics VCU, Department of Computer Science CMSC 302 Trees Vojislav Kecman Terminology Trees as Models Some Tree Theorems Applications of Trees Binary Search Tree Decision Tree Tree Traversal Spanning Trees

More information

UNIT 4 Branch and Bound

UNIT 4 Branch and Bound UNIT 4 Branch and Bound General method: Branch and Bound is another method to systematically search a solution space. Just like backtracking, we will use bounding functions to avoid generating subtrees

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

Graphs Data Structures

Graphs Data Structures Graphs Data Structures Introduction We looked previously at the binary tree data structure, which provides a useful way of storing data for efficient searching. In a binary tree, each node can have up

More information

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Branch and Bound. Live-node: A node that has not been expanded. It is similar to backtracking technique but uses BFS-like search.

Branch and Bound. Live-node: A node that has not been expanded. It is similar to backtracking technique but uses BFS-like search. Branch and Bound Definitions: Branch and Bound is a state space search method in which all the children of a node are generated before expanding any of its children. Live-node: A node that has not been

More information

Foundations of Discrete Mathematics

Foundations of Discrete Mathematics Foundations of Discrete Mathematics Chapter 12 By Dr. Dalia M. Gil, Ph.D. Trees Tree are useful in computer science, where they are employed in a wide range of algorithms. They are used to construct efficient

More information

COMP 251 Winter 2017 Online quizzes with answers

COMP 251 Winter 2017 Online quizzes with answers COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots

More information

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms

More information

Practice Problems for the Final

Practice Problems for the Final ECE-250 Algorithms and Data Structures (Winter 2012) Practice Problems for the Final Disclaimer: Please do keep in mind that this problem set does not reflect the exact topics or the fractions of each

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia) 15-251 Great Ideas in Theoretical Computer Science Lecture 12: Graphs I: The Basics February 22nd, 2018 Crossing bridges Königsberg (Prussia) Now Kaliningrad (Russia) Is there a way to walk through the

More information

Graph Representations and Traversal

Graph Representations and Traversal COMPSCI 330: Design and Analysis of Algorithms 02/08/2017-02/20/2017 Graph Representations and Traversal Lecturer: Debmalya Panigrahi Scribe: Tianqi Song, Fred Zhang, Tianyu Wang 1 Overview This lecture

More information

Backtracking and Branch-and-Bound

Backtracking and Branch-and-Bound Backtracking and Branch-and-Bound Usually for problems with high complexity Exhaustive Search is too time consuming Cut down on some search using special methods Idea: Construct partial solutions and extend

More information

Solutions to relevant spring 2000 exam problems

Solutions to relevant spring 2000 exam problems Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for

More information

CS/COE

CS/COE CS/COE 151 www.cs.pitt.edu/~lipschultz/cs151/ Graphs 5 3 2 4 1 Graphs A graph G = (V, E) Where V is a set of vertices E is a set of edges connecting vertex pairs Example: V = {, 1, 2, 3, 4, 5} E = {(,

More information

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency

More information

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Red-Black Trees Graph Algorithms Many slides here are based on E. Demaine, D. Luebke slides Binary Search Trees (BSTs) are an important data structure for dynamic sets In addition to satellite

More information

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Module 1 OBJECTIVE: Algorithms play the central role in both the science and the practice of computing. There are compelling reasons to study algorithms.

More information

Applications of BDF and DFS

Applications of BDF and DFS January 14, 2016 1 Deciding whether a graph is bipartite using BFS. 2 Finding connected components of a graph (both BFS and DFS) works. 3 Deciding whether a digraph is strongly connected. 4 Finding cut

More information

Topic 12: Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms Topic 12: Elementary Graph Algorithms Pierre Flener Lars-enrik Eriksson (version of 2013-02-10) Different kinds of graphs List Tree A B C A B C Directed Acyclic Graph (DAG) D A B C D E Directed Graph (Digraph)

More information

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) ( ) C.  1 n. ( ) $ f n. ( ) B.  log( n! ) ( ) and that you already know ( ) ( )  % g( n) ( )  #& CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i

More information

Greedy Approach: Intro

Greedy Approach: Intro Greedy Approach: Intro Applies to optimization problems only Problem solving consists of a series of actions/steps Each action must be 1. Feasible 2. Locally optimal 3. Irrevocable Motivation: If always

More information

Trees. Eric McCreath

Trees. Eric McCreath Trees Eric McCreath 2 Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for:

More information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if

More information

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Trees and Graphs Basic Definitions Tree: Any connected, acyclic graph G = (V,E) E = V -1 n-ary Tree: Tree s/t all vertices of degree n+1 A root has degree n Binary Search Tree: A binary tree such that

More information

SELF-BALANCING SEARCH TREES. Chapter 11

SELF-BALANCING SEARCH TREES. Chapter 11 SELF-BALANCING SEARCH TREES Chapter 11 Tree Balance and Rotation Section 11.1 Algorithm for Rotation BTNode root = left right = data = 10 BTNode = left right = data = 20 BTNode NULL = left right = NULL

More information

Lecture 9 Graph Traversal

Lecture 9 Graph Traversal Lecture 9 Graph Traversal Euiseong Seo (euiseong@skku.edu) SWE00: Principles in Programming Spring 0 Euiseong Seo (euiseong@skku.edu) Need for Graphs One of unifying themes of computer science Closely

More information

CS6301 Programming and Data Structures II Unit -5 REPRESENTATION OF GRAPHS Graph and its representations Graph is a data structure that consists of following two components: 1. A finite set of vertices

More information

Konigsberg Bridge Problem

Konigsberg Bridge Problem Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at

More information