Lecture 6 BigO notation Linked lists Outcomes n
Lecture 6 Big-O notation Linked lists
Outcomes n Understand Big-O notation n Understand shortcomings of the Arrays n Understand nature and structure of Linked lists
Big-O notation n also known as ¨ big Oh notation, ¨ big Omicron notation, ¨ Landau notation, ¨ Bachmann–Landau notation, ¨ asymptotic notation
Big-O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. n Big O specifically describes the worstcase scenario, and can be used to describe the execution time required or the space used (e. g. in memory or on disk) by an algorithm. n
O(1) n O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set.
O(1) Function Is. First. Element. Empty(s as String()) As Boolean If s(0) = "" Then Return True Else Return False End If End Function
O(N) n O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set.
O(N) Function Contains. Value(s As String(), v As String) As Boolean For i As Integer = 0 To s. Length - 1 If s(i) = v Then Return True End If Next Return False End Function
O(N 2) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. n This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N 3), O(N 4) etc. n
O(N) Private Function Contains. Duplicates(By. Val s As String()) As Boolean For i As Integer = 0 To s. Length - 1 For j As Integer = 0 To s. Length - 1 If i = j Then 'Don't compare with self Continue For End If If s(i) = s(j) Then Return True End If Next Return False End Function
Other possibilities O(log N) – binary search n “Have a guess” exercise n
Shortcomings of the Arrays Insertion and Deletion requires shifting of all array elements n Need to know the size in advance n
Linked List A linked list is a collection of class objects called nodes. n Each node is linked to its successor node in the list using a reference to the successor node. n A node consists of a field for storing data and the field for the next node reference. n The reference to another node is called a link. n
Anatomy of a linked list n A linked list consists of: ¨A sequence of nodes my. List a b c Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list may have a header d
Linked List n Whereas the elements in an array are referenced by position (the index), the elements of a linked list are referenced by their relationship to the other elements of the list.
More terminology n A node’s successor is the next node in the sequence ¨ The n A node’s predecessor is the previous node in the sequence ¨ The n last node has no successor first node has no predecessor A list’s length is the number of elements in it ¨A list may be empty (contain no elements)
Advantages of Linked Lists Insertion becomes a very efficient task when using a linked list – O(1). n Removing an item from a linked list is just as easy – O(1). n Index – O(N) n
Circularly linked list n A circularly linked list is a list in which the last node points back to the first node (which may be a header node).
Doubly linked list
Implementation in VB. NET Linked. List(Of T) Class n Linked. List. Node(Of T) Class n Doubly linked n
Reading n Data structures and Algorithms using VB. NET, chapter 11 on Linked Lists
The End
- Slides: 22