Traversal and searching

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

seqSearch(list L, targetType X, boolean found, int location

1: found ← false 2: location ← 1 3: while not found and location ≤ max do 4: if L[location] = X then 5: found ← true 6: else 7: location ← location + 1 8: end if 9: end while

seqSearch(node L, targetType X, boolean found, node location

1: found ← false 2: while (not found) and (L 6= null) do 3: if L.da = X then 4: found ← true 5: location ← L 6: else 7: L ← L.next 8: end if 9: end while

BinarySearch1(list L, boolean found, index location)

1: high ← n, low ← 1 2: while high > low do 3: mid ← (low + high) div 2 4: if X > L[mid] then 5: low ← mid + 1 6: else 7: high ← mid 8: end if 9: if high = 0 then 10: found ← false 11: else 12: found ← (X = L[high]) 13: end if 14: end while 15: if found then 16: location ← high 17: end if

BinarySearch2(list L, boolean found, index location

1: high ← n, low ← 1, found ← false 2: while (not found) and (high ≥ low) do 3: mid ← (low + high) div 2 4: if X < L[mid] then 5: high ← mid − 1 6: else if X > L[mid] then 7: low ← mid + 1 8: else 9: found ← true 10: end if 11: end while 12: if found then 13: location ← high 14: end

insert(data X, searchTree T )

1: if T = null then 2: T.data ← X 3: T.left ← null 4: T.right ← null 5: else if X < T.data then 6: insert(X, T.left) 7: else if X > T.data then 8: insert(X, T.right) 9: end if {Do nothing if Xis already in T (X = T.data)}

treeNode findNode(data X, searchTree T )

1: if T = null then 2: return null 3: else if X = T.data then 4: return T 5: else if X < T.data then 6: return findNode(X, T.left) 7: else 8: X > T.data 9: return findNode(X, T.right) 10: end if

bSearchDraft(index low, high, location) (to complete)

1: if low < high then 2: find centre position mid 3: compare X with L[mid] 4: if X < L[mid] then 5: bSearch(low,mid − 1) 6: else if X > L[mid] then 7: bSearch(mid + 1, high) 8: else 9: location ← mid 10: end if 11: end if

BFS(headerList adjacencyList, vertexType v)

1: initialise(Q) 2: visit and mark v, enqueue(Q, v) 3: while not empty(Q) do 4: dequeue(Q, x) 5: for each unmarked vertex w adjacent to x do 6: visit and mark w 7: enqueue(Q, w) 8: end for 9: end while

DFS(headerList adjacencyList, vertexType v)

1: initialise(S) 2: visit, mark, and push(S, v) 3: while not empty(S) do 4: while there is an unmarked vertex w adjacent to top(S) do 5: visit, mark, and push(S, w) 6: pop(S, x) 7: end while 8: end while

DFS(vertexType v)

1: visit and mark v 2: while there is an unmarked vertex vertexType w adjacent to v do 3: DFS(w) 4: end while

What are the disadvantages of BFS?

A BFS on a binary tree generally requires more memory than a DFS.

What are the advantages of BFS?

A BFS will find the shortest path between the starting point and any other reachable node. A depth-first search will not necessarily find the shortest path.

Selection Sort

An algorithm which passes over the list, finds the smallest item and moves it to the left, then repeats the exercise for the remaining list until it is all sorted.

if

a Python keyword that makes a selection

for

a Python keyword that starts a loop

Given an array S[1..10], initialise the array to 0s.

1: for i ← 1; i ≤ 10; i + + do 2: S[i] ← 0 3: end for

Bubble Sort

A Sorting Algorithm that passes over the list many times, compares each pair of adjacent items and swaps them if they are in the wrong order.

What is Binary Search?

A binary search algorithm finds an item in a sorted array in O(log n) time.

Selection

A code construct that makes a choice between two or more outcomes

Selection

A code construct that makes a choice between two or more outcomes - IF / ELIF

Iteration

A code construct, also known as a loop.

Applications of DFS: Finding Strongly Connected Components of a graph

A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex.

Applications of DFS: Detecting cycle in a graph

A graph has cycle if and only if we see a back edge during DFS. So we can run DFS for the graph and check for back edges.

Variable

A named value in a computer program that can be changed by the program code as it runs. "temp" and "num" are examples in our bubble sort program.

Sorting Algorithm

A process commonly used to sort data

Binary Search

A search algorithm that divides the search space in half each time until it finds the target, faster than linear but requires the array to be sorted.

Linear Search

A search algorithm which looks at every element in turn until it finds the target, it is slow but works even on unsorted data.

List

A set of data that can be sorted into order

Merge Sort

A sorting algorithm that sorts partial lists then merges them together.

Search Algorithm

A structured process that finds a target in a set of data.

Array

A variable that can hold list items

Array

A variable that can hold multiple items under one name

What are the advantages of DFS?

Advantages: Depth-first search on a binary tree generally requires less memory than breadth-first. Depth-first search can be easily implemented with recursion.

How can you use Binary Search built in in Java?

Arrays.binarySearch(data_type arr, data_type key) is the simplest and most efficient method to find an element in a sorted array in Java data_type can be any of the primitive data types: byte, char, double, int, float, short, long and Object as well. This returns the index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Explain BFS again

BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes which are directly connected to source node). Therefore, in BFS, you must traverse all the nodes in layer 1 before you move to the nodes in layer 2.

Binary search trees

Binary search trees (BST) are binary trees with an order property. They are particularly useful for searching. Each node in a binary search tree contains at least one key field of some rankable value. For every node Y in the tree, the values of all the keys in the left subtree are smaller than the key value of Y , and the values of all the keys in the right subtree are larger than the key value of Y .

Applications of BFS: GPS Navigation systems

Breadth First Search is used to find all neighboring locations

What is BFS?

Breadth-first search (BFS) is a method for exploring a tree or graph. In a BFS, you first explore all the nodes one step away, then all the nodes two steps away

Breadth-First

Complete? Yes, if b is finite Time: O(b^d) Space: O(b^d) Optimal: Yes, if all step costs are identical, or if path cost non-decreasing function of depth only

Applications of BFS: Crawlers in Search Engines:

Crawlers build index using Breadth First. The idea is to start from source page and follow all links from source and keep doing same. Depth First Traversal can also be used for crawlers, but the advantage with Breadth First Traversal is, depth or levels of built tree can be limited.

Applications of DFS: Solving puzzles with only one solution, such as mazes

DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.

What is DFS?

Depth-first search (DFS) is a method for exploring a tree or graph. In a DFS, you go as deep as possible down one path before backing up and trying a different one

What are the disadvantages of DFS?

Disadvantages A DFS doesn't necessarily find the shortest path to a node, while breadth-first search does.

Descending

Falling, going from largest to smallest

traversal mechanism

Here the term visit can be interpreted in many ways with various meanings. For example, printing out each value can be a traversal, and updating such as adding, subtracting, multiplication, division, assigning 0s can be another traversal

What does backtracking mean in DFS?

Here, the word backtrack means that when you are moving forward and there are no more nodes along the current path, you move backwards on the same path to find nodes to traverse. All the nodes will be visited on the current path till all the unvisited nodes have been traversed after which the next path will be selected.

Algorithm analysis

If a binary search tree is balanced, the function findNode takes O(log n) time in the worst case. This is because, each time, we descend a level in the tree, thus operating on a tree that is now about half as large. Recall that a balanced binary tree is the tree in which the depths of the leaves are all the same. However, a binary search tree can be so "bad" that it does not have any branching. For example, if the integers are inserted in ascending order using the procedure insert (Algorithm 5.10) started with an empty tree. In this case, the height of the binary search tree is O(n). Hence the function findNode needs O(n) time in the worst case.

Binary search

If the list is pre-sorted, then a more efficient way of searching strategy called binary search can be used. The idea of binary search is to first compare the key with one at the centre of the list1 and 1or as close to the centre as possible if there are an even number of entries. then move our attention to only one of the first or the second half of the list. In this way, at each step we reduce the length of the list to be searched by approximately half. Example 5.4 L = (3, 7, 11, 12, 15, 19, 24, 33, 41, 55),X = 20. i 1 2 3 4 5 6 7 8 9 10 centre element L[i] 3 7 11 12 15 19 24 33 41 55 15 20 L[i] 19 24 33 41 55 33 20 L[i] 19 24 19 20 L[i] 24 24 20 20 is not found!

Applications of BFS: Peer to Peer Networks

In Peer to Peer Networks like BitTorrent, Breadth First Search is used to find all neighbor nodes.

searching algorithms

In order to focus on the main issues of the searching problem, we further assume that: 1. All the elements in the given list L are unique 2. X and any element in L are comparable. Our goal is to minimise the number of comparisons such as <, = or > during searching

Applications of BFS: Social Networking Websites

In social networks, we can find people within a given distance 'k' from a person using Breadth First Search till 'k' levels.

Quick Sort

In this sorting algorithm, a pivot is chosen, and all the elements moved either side of the pivot. This is repeated with another pivot either side, recursively until done.

Applications of BFS: Cycle detection in undirected graph:

In undirected graphs, either Breadth First Search or Depth First Search can be used to detect cycle. In directed graph, only depth first search can be used.

Applications of BFS: Explain how BFS can be used to find the shortest path in an unweighted graph

In unweighted graph, the shortest path is the path with least number of edges. With Breadth First, we always reach a vertex from given source using minimum number of edges.

Searching divides naturally into two categories depending on where the records to be searched are stored, namely

Internal searching: the records are stored entirely within the computer memory, directly accessible by the processor in fixed time. External searching: the large amount of records have to be stored in files on disks or tapes.

What is the time complexity of DFS when using an adjacency list?

O(V+E), when implemented using the adjacency list, where V is vertices and E is edges

a[n]

Python code that represents the nth member of an array called a.

Insertion Sort

Removes one element from unsorted data and puts it where it belongs in sorted list. Repeats until no input elements remain.

Ascending

Rising, going from smallest to largest

Searching

Searching is a class of problems for which the goal is to discover whether or not a particular element is contained in a collection. One of the most important functions of any computer application is information retrieval, from a small database allows simple queries to a web-based search engine such as Google. The main step in information retrieval is searching.

Explain DFS again

The DFS algorithm is a recursive algorithm that uses the idea of backtracking. It involves exhaustive searches of all the nodes by going ahead, if possible, else by backtracking.

Complexity analysis

The analysis is straightforward. 1. Worst case: n comparisons (where n is the number of elements in the list to be searched). This is when X appears only in the last position in the list or when X is not in the list at all. 2. Average case: (n + 1)/2 comparisons. Since X could be in any position of 1..n with equal probability, the number of comparisons done would be 1..n respectively. So on average, the number of comparisons is 1+2+···+n n = n+1 2 . So the sequential search algorithm takes O(n) time in both worst case and average case

Breadth-first traversal

The breadth-first traversal is to visit all vertices within one same radius (the same path length from the start point) before visiting all vertices within the next radius further

efficiency of searching

The efficiency of searching depends on the relationships within a large set of data records, each of which consists of a number of fields. The field is called a key of the record if it can be used to identify each record uniquely. It is often time-consuming to search for a given key value to locate a record from a large data collection, especially if the searching needs to be performed frequently. Thus the research on how to arrange the record storage and what is the best method for searching is intensive.

Graph traversal

The graph traversal problem can be broadly divided into two types: one is to visit every node of a graph and the other is to traverse every edge of a graph. We consider only simple graphs, i.e. graphs which contain no self-loops or parallel edges, in this subject guide.

Target

The item we are searching for in a search algorithm.

Depth-first traversal

The main idea of depth-first traversal is to go as far as possible along a path without revisiting any node, then backtrack to the last turning point and go as far as possible down the next path, and so on, until all the nodes are visited.

Traversal on a linear data structure

The problem of traversal of a linear data structure such as an array, queue, stack or set is easy. A single loop can be used to have each datum visited in the structure.

Sequential search

The simplest way to search for a key in a list is to scan the whole list, from the start to the end, until the key is found or the finish end is reached. This is called sequential search. Example 5.3 L = (12, 34, 2, 9, 7, 5), X = 7. We compare the key "7" with each element in the list from left to right. 12 34 2 9 7 5 7 12 34 2 9 7 5 7 12 34 2 9 7 5 7 12 34 2 9 7 5 7 12 34 2 9 7 5 7 found!

What is the time complexity of BFS to traverse a graph?

Time complexity of both BFS will be O(V + E), where V is the number of vertices, and E is the number of Edges. This again depends on the data structure that we user to represent the graph. If it is an adjacency matrix, it will be O(V^2) . If we use an adjacency list, it will be O(V+E).

Applications of DFS: Topological Sorting

Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs. In computer science, applications of this type arise in instruction scheduling

traversal step

Traversal is a fundamental step in many other more complicated operations, such as checking how many data exceed a certain threshold. In particular, traversal is necessary for any searching algorithm in the worst case. For this reason, some traversal algorithms are described as searching algorithms. Examples include the popular depth-first search and breadth-first search

Pivot

Used in Quick Sort, items are compared to this element, and placed one side or the other.

Applications of DFS: To test if a graph is bipartite

We can augment either BFS or DFS when we first discover a new vertex, color it opposited its parents, and for each other edge, check it doesn't link two vertices of the same color. The first vertex in any connected component can be red or black!

Applications of DFS: Path Finding

We can specialize the DFS algorithm to find a path between two given vertices u and z.

travesal algorithms

We look at two traversal algorithms on a graph, namely depth-first and breadth-first traversal. They are also called depth-first search and breadth-first search historically. The word traversal is sometimes interchangeable with search in this context since traversal problems are so closely related to searching problems. Strictly speaking, the word search is different from traverse. A search will stop as soon as the searched item is found, but traversal will visit every item in the data structure.

IF

a Python keyword that makes a selection

FOR

iteration - loop for a specific number of times

Traversal

traversal problem can be described as the situation where every datum in a given data structure needs to be visited at least once.


Set pelajaran terkait

Com Apps: What is Human Communication? quiz

View Set

Ch 42-45 reactions and functional groups

View Set

Cannon Post Test Study Guide 1-5

View Set

PH Chp. 21- The Nurse-led Health Center: A Model for Community Nursing Practice

View Set