/ DLList.c - Implementation of doubly-linked list ADT
/ Written by John Shepherd, March 2013
/ Modified by John Shepherd, August 2014, August 2015
#define FIRST 1
#define LAST 2
#include
#include
#include
#include
#include
<stdlib.h>
<stdio.h>
<string.h>
Divide and Conquer Sorting Algorithms
COMP1927 15s1
Sedgewick Chapters 7 and 8
DIVIDE AND CONQUER SORTING ALGORITHMS
Step 1
If a collection has less than two elements, its already
sorted
Otherwise, split it into two parts
Step 2
Sort both parts separ
Graph Search
Computing 2 COMP1927 15s1
PROBLEMS ON GRAPHS
What kinds of problems do we want to solve on/via
graphs?
Is there a simple path from A to B
Is the graph fully-connected?
Can we remove an edge and keep it fully connected?
Which vertices are r
COMP1927 15s1
Computing 2
Linked Lists and Function
Pointers
1
What is a linked list?
sequential collection of items (i.e. no random
access)
we can only get to the second by accessing the
first, and so on
we cant access the nth element of a list directly
SEARCHING AND TRIES
COMP1927
Computing 15s1
TRIES
The name trie come from the word retrieval
Tries are an instance of radix search.
Tries are useful for prefix-search of strings
Each node in a trie (implementing a dictionary)
Eg as might be useful in i
Directed Graphs
Computing 2 COMP1927 15s1
DIRECTED GRAPHS
In our previous discussion of graphs:
an edge indicates a relationship between two vertices
an edge indicates nothing more than a relationship
In many real-world applications of graphs:
edges are d
Compilation and Debugging
Computing 2
15s1
THE C COMPILER (GCC)
applies source-to-source transformation (preprocessor)
compiles source code to produce object files
links object files and libraries to produce
executables
COMPILATION AND LINKING WITH GCC
Priority Queues and Heaps
Computing 2 COMP1927 15s1
PRIORITY
Some applications of queues require items
processed in order of "key" or priority
rather than in order of entry (FIFO)
Priority Queues (PQueues or PQs) provide this via:
insert item into PQ
le
ABSTRACT DATA TYPES (ADTS)
COMP1927
Computing 2 15s1
Sedgewick Chapter 4
ABSTRACTION
To understand a system, it should be enough to
understand what its components do without knowing how
Watching a television
We operate the tv through its interface remot
Graph Algorithms and
Computability
Computing 2 COMP1927 15s1
HAMILTON PATH
Hamilton path:
Hamilton tour:
is there a simple path connecting two vertices
that visits each vertex in the graph exactly
once?
is there a cycle in the graph that visits each
verte
Hazard
Risk
Safety and Precaution
Glassware Test tube
Test Tube may break and
cause cuts.
Handle with care.
Use a test tube rack to prevent test tubes from falling of workbench.
Notify demonstrator of broken glassware and follow appropriate clean up proto
Recursion
COMP1927 15s1
Sedgewick Chapter 5
RECURSIVE FUNCTIONS
Function decomposition:
problems can be expressed in terms of simpler problems
Recursion
problems can sometimes be expressed in terms of a simpler
instance of the same problem
Example: factor
HASH TABLES
HASHING
Key indexed arrays had perfect search performance
O(1)
But required a dense range of index values
Otherwise memory is wasted
Hashing allows us to approximate this performance
but
Allows arbitrary types of keys
Map(hash) keys into compa
Balanced Trees
Computing 2 COMP1927 15s1
BST PERFORMANCE ISSUES
Performance of insert and search good on average,
but poor for some (common) special cases (items
inserted in sorted order O(n)
Goal:
A perfectly balanced binary tree (weight/size
balanced)
Graphs
Computing 2 COMP1927 15s1
Sedgewick Part 5: Chapter 17
WHAT ARE GRAPHS
Many applications require a collection of items (i.e.
a set)
relationships/connections between items
Examples: maps: items are cities, connections are
roads
web: items are pag
Weighted Graphs
Computing 2 COMP1927 15s1
Sedgewick Part 5: Chapter 20.1 -20.4
21.1 - 21.3
WEIGHTED GRAPHS
Some applications require us to consider a cost or
weight
Often use a geometric interpretation of weights
costs/weights are assigned to edges
low we
Even MORE Balanced Trees
Computing 2 COMP1927 15s1
OPTIMISATION BASED BALANCED TREES
So far we have seen local approaches
Randomised treesmake poor performance unlikely
Splay treesreasonable amortized performance
BUT both still have O(n) worst case.
Ideal
SEARCHING AND TREES
COMP1927
Computing 15s1
Sedgewick Chapters 5, 12
SEARCHING
Storing and searching sorted data:
Dilemma: Inserting into a sorted sequence
Finding the insertion point on an array O(log n) but
then we have to move everything along to cr
COMP1927 15s1
Computing 2
Complexity
1
Problems, Algorithms, Programs
and Processes
Problem: A problem that needs to be
solved
Algorithm: Well defined instructions for
completing the problem
Program: Implementation of the algorithm
in a particular program
Symbol Tables
Computing 2 COMP1927 15s1
SYMBOL TABLES
Searching: like sorting, searching is a fundamental
element of many computational tasks
data bases
dictionaries
compiler symbol tables
Symbol table: a symbol table is a data structure of
items with key
Non-comparison based Sorting Algorithms
COMP1927 15s1
Sedgewick Chapter 6.10, Chapter 10
HOW FAST CAN A SORT BECOME?
All the sorts we have seen so far have been
comparison based sorts
find order by comparing elements in the sequence
can sort any type of d
Elementary Sorting Algorithms
COMP1927 15s1
Sedgewick Chapter 6
WARM UP EXERCISE: HANDSHAKE PROBLEM
In a room of n people, how many different
handshakes are possible?
0 + 1 + 2 + + (n-1)
Using Maths formula:
1 + 2 + n = (n(n+1)/2
Answer: (n-1)n)/2 = (n^2-
Results for Tests on 5000 values
Depth = depth of tree, Cost = average #comparisons per search
Ascending Order Prefix Order Random Order
Insert
Method Depth Cost Depth Cost Depth Cost
L _ _ _ _ _ _
A _ _ _ _ _ _
R _ _ _ _ _ _
B _ _ _ _ _ _
S _ _ _ _ _ _
/ tlab.c . Tree Lab main program
/ Builds trees, collects stats
/ Written by John Shepherd, March 2013
#include <stdlib.h>
#include <stdio.h>
#include "Tree.h"
/ all values are in range 0000.9999
#define RANGE 10000
void usage();
void mkprefix(int *, int,
Know Thy Complexities!
www.bigocheatsheet.com
Big-O Complexity Chart
Excellent
O(n!) O(2^n)
Good
Fair
Bad
Horrible
O(n^2)
Operations
O(n log n)
O(n)
O(1), O(log n)
Elements
Common Data Structure Operations
Data Structure
Space
Complexity
Time Complexity
A
Notes:
C -> 72-73/94 ; short summary of sorts
BSTs:
- depth-first : searching for specific value (in BSTs)
- breadth-first : shortest path between two nodes
(can be applied to any graph)
- Rebalancing:
> Left rotation: move left child to root; rearrange
l
Experiment Report
Topic: Sort Detective
Authors: Hai Long Bi and Jingcheng Li
Date:
8th April 2017
Experimental Objective
Design, plan and document the set of tests, to deduce the right sort algorithm from all possible
algorithms.
Experimental Principles
/ travel.c . client for Graph ADT
/ Written by John Shepherd, May 2013
/group partners are Hai Long Bi and Jingcheng Li
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Graph.h"
/ make some assumptions on much data is supplied
#define
/ Graph.c . implementation of Graph ADT
/ Written by John Shepherd, May 2013
/group partners are Hai Long Bi and Jingcheng Li
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "Graph.h"
#include "Queue.h"
/ graph repr