Traversal and searching

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

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

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

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 .

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

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!

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

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.

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.

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.

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!

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

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.

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

RN Comprehensive online practice 2019 A with NGN-tap

View Set

Los 9 Tipos de Gimnasia y sus Características

View Set

The Federal Bureaucracy Quiz - 100%

View Set

Introduction to Java Chapter 1 Worksheet

View Set

Adult Health I Exam 1 Practice Questions

View Set