Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011



Similar documents
TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Programming with the Dev C++ IDE

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

1 Abstract Data Types Information Hiding

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

University of Dayton Department of Computer Science Undergraduate Programs Assessment Plan DRAFT September 14, 2011

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

The programming language C. sws1 1

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

How to Write a Simple Makefile

CP Lab 2: Writing programs for simple arithmetic problems

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Getting Started with the Internet Communications Engine

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Simple C Programs. Goals for this Lecture. Help you learn about:

Embedded Software Development

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

The Function Pointer

Lecture 27 C and Assembly

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Illustration 1: Diagram of program function and data flow

Introduction to Data Structures

RVDS 3.x with Eclipse IDE

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Sources: On the Web: Slides will be available on:

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Trees and Huffman Encoding Binary Search Trees

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Software Engineering and Service Design: courses in ITMO University

C Programming Language

Static Code Analysis Procedures in the Development Cycle

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Building and Using a Cross Development Tool Chain

SQLITE C/C++ TUTORIAL

Code Estimation Tools Directions for a Services Engagement

Data Structures in the Java API

Number Representation

Eddy Integrated Development Environment, LemonIDE for Embedded Software System Development

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Basic Programming and PC Skills: Basic Programming and PC Skills:

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

csce4313 Programming Languages Scanner (pass/fail)

Base Conversion written by Cathy Saxton

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

A Comparison of Dictionary Implementations

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Informatica e Sistemi in Tempo Reale

Ubuntu, FEAP, and Virtualiza3on. Jonathan Wong Lab Mee3ng 11/08/10

Building Software Systems. Multi-module C Programs. Example Multi-module C Program. Example Multi-module C Program

C Programming Review & Productivity Tools

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Parallelization: Binary Tree Traversal

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Project: Simulated Encrypted File System (SEFS)

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Control III Programming in C (small PLC)

1/20/2016 INTRODUCTION

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

DNA Data and Program Representation. Alexandre David

Linked Lists Linked Lists, Queues, and Stacks

Building Embedded Systems

MarshallSoft AES. (Advanced Encryption Standard) Reference Manual

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Lecture Notes on Binary Search Trees

Data Structure Reverse Engineering

Streamline Computing Linux Cluster User Training. ( Nottingham University)

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

Binary Heap Algorithms

Installing Java. Table of contents

CS 300 Data Structures Syllabus - Fall 2014

Easing embedded Linux software development for SBCs

Lecture 2: Data Structures Steven Skiena. skiena

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

COMPUTER SCIENCE 1999 (Delhi Board)

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Computer Programming C++ Classes and Objects 15 th Lecture

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Lecture Notes on Binary Search Trees

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

Transcription:

Applied Programming and Computer Science, DD2325/appcs5 PODF, Programmering och datalogi för fysiker, DA7 Autumn 25 Lecture 6, Huffman coding and hashing A. Maki, C. Edlund Tree When are trees used? Binary heap / Heap sort Data compression Databases File system Compiler uses syntax trees Chess program Decision trees... Binary heap A binary heap is a binary tree with two additional constraints Heap property: All nodes are greater(less) than or equal to each of its children Shape property: All levels of the tree except the deepest are filly filled (complete binary tree) Compression, Huffman code Every character is coded binary. The common characters have short code. Less common, longer code. Statistically decided. Example TREESTRUCTURE The characters T, R and E are most common and therefore have short code. The tree structure for the example: Siblings in a heap can be interchanged unless doing so violates the constraints. How can we build a heap from a set of data? Given a binary heap, Heap sort algorithm is straightforward. T S E U C R

Hashing Compression, Huffman code (cont.) How can TREESTRUCTURE be represented in binary code? How many bits are required in total? Compare the length with ASCII (using a byte) which ends up with: 3*8 = 4 bits Hashing is a way to obtain both efficient memory use and effective retrieval. The keyvalue of a record is used to calculate the address where the record is to be stored. Records: Keyvalue Data Data Keyvalue 5 Keyvalue 4 Keyvalue 3 Keyvalue 2 more data Compression is used when zipping files, to compress pictures/movies (files)... Calculate: more data hashfunction(keyvalue) address Storage: 3.... address 2 4 5.. Hash function The requirements one can have on the hash function: must calculate an address within the legal address space. addresses should be evenly distributed in the legal address space. easy to calculate an address. The hash function can result in the same address for different keys. The collisions can be dealt with open or closed address hashing. Open address hashing: Open and closed address hashing Linear probing. If the keyvalue has given the address to an already occupied space the next adress space is considered. Closed address hashing: Chained hashing. Every entry in the storage is a list. All records with the same address ends up in the same list.

Compiling & linking From problem to executable file Editor emacs Thinking A problem is formulated, algorithms devised and data structures chosen. Programming The C program is written using an editor (emacs, xemacs). Source files file.c file2.c file3.c Preprocessing Compiling Linking Debugging Preprocessor directives are interpreted and a preprocessed C file generated (gcc -E, cc -E, cpp). The preprocessed source code is translated to object code (machine instructions) (gcc -c, cc -c). The object code is linked with external libraries to produce an executable (gcc, cc, ld). Make sure the program works as expected. Correct possible mistakes (gdb). Preprocessor Compiler Object files file.o file2.o file3.o The first step is the most important! By considering the problem carefully the debugging time can be significantly reduced. Linker External libraries Executable file a.out Compiling & linking (cont.) At CSC there are two different C compilers, cc (Sun) and gcc (GNU). Both can be used for preprocessing, compiling and linking small programs in a single command. gcc file.c -o runme generates an executable runme from the source code in file.c. The object files are linked with the most common libraries (stdio, stdlib and possibly a few more). If compiling a program is complicated (several different files, many options, different programming languages,... ) it is convenient to use Makefiles. Compiler & linker options In some cases additional information must be supplied for the compiler to generate an executable. These are given as command line options. There are also some options useful for debugging and optimizing. Option gcc cc Effect -c -c Compile source files but do not link -llib -llib Link with library lib -Ldir -Ldir Add dir to library search path -Idir -Idir Add dir to include file search path -otgt -otgt Name executable tgt (default:a.out) -O2 -fast Compile with optimization -Wall -v Print extra warning messages For a more complete list of options, give the command: man gcc (or man cc) at the prompt.

Compiling examples Compile program.c and generate executable target with optimization, > gcc program.c -O2 -o target Same example, but linked with math library > gcc program.c -O2 -o target -lm Compile files main.c and function.c and link to obtain executable runme > cc -c main.c > cc -c function.c > cc function.o main.o -o runme -lm User-defined include files and libraries, > cc program.c -L~/mylibraries -I~/myincludefiles -lmylib ( > is used to indicate that the command is given at the prompt.) #include <stdio.h> #include <stdlib.h> #include "stack.h" stack.c int isempty(listnodeptr sptr){return sptr == NULL;} int isfull(listnodeptr sptr){return ;} void push(listnodeptr *sptr, char value){ ListNodePtr newptr;... See lecture notes 5} char pop(listnodeptr *sptr){ ListNodePtr currptr; char value;... See lecture notes 5} void printlist (ListNodePtr currptr){... See lecture notes 4} struct listnode { char data; struct listnode *nextptr; }; stack.h typedef struct listnode ListNode; typedef ListNode *ListNodePtr; int isempty(listnodeptr sptr); int isfull(listnodeptr sptr); void push(listnodeptr *sptr, char value); char pop(listnodeptr *sptr); void printlist (ListNodePtr currptr); #include <stdio.h> #include <stdlib.h> #include "stack.h" main.c main(){ ListNodePtr startptr = NULL; char item; int noofnodes = ; printf("write data: "); scanf("\n%c",&item); while (item!= q ) { push(&startptr, item); printf("write data: "); scanf("\n%c",&item); noofnodes ++; } printf("%d\n", noofnodes); printf("the contents of the stack: \n"); printlist(startptr);} % gcc main.c stack.c -o runme

Appendix: Encouraging good coding Techniques for achieving good coding practices (in a project) Assign two people to every part of the project Review every line of code Require code sign-offs Emphasize that code listings are public assets Reward good code (Code Complete: A practical handbook of software construction, Steve McConnell)