Programming Application in Civil Engineering

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Bioinformatics Programming
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks Example: Stack of plates in cafeteria.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
DATA STRUCTURE RECAP Yiqun Zhang University of Houston.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Section 8.1 – Systems of Linear Equations
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Introduction to Data Structure
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Ceng-112 Data Structures I Chapter 6 Recursion.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Polynomial Addition using Linked lists
INTRODUCTION TO DATA STRUCTURES. INTRODUCTION A data structure is nothing but an arrangement of data either in computer's memory or on the disk storage.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structures & Algorithms
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Example Program Development
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Introduction Of Stack.
Top 50 Data Structures Interview Questions
Computer Programming BCT 1113
More Linking Up with Linked Lists
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
CSCE 3110 Data Structures & Algorithm Analysis
Abstract Data Types Sparse Matrices CSCI 240
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Recursion &Faster Sorting
STACKS.
Stacks Stack: restricted variant of list
Data Structures – Week #3
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
Reminder: Array Representation In C++
CSCE 3110 Data Structures & Algorithm Analysis
Reminder: Array Representation In C++
Unit-5 Dynamic Programming
Further Data Structures
The Hanoi Tower Problem
Stack.
Data Structures – Week #3
Matrices An appeaser is one who feeds a crocodile—hoping it will eat him last. Winston Churchhill.
Section 8.1 – Systems of Linear Equations
Reminder: Array Representation In C++
Presentation transcript:

Programming Application in Civil Engineering - Application of Data Structure

Sparse Matrix Matrices with relatively high proportion of zero or null entries are called sparse matrices. When matrices are sparse, then much space and computing time could be saved if the non-zero entries were stored explicitly i.e. ignoring the zero entries the processing time and space can be minimized in sparse matrices. In this matrix we have 6 rows and 7 columns. There are 5 nonzero entries out of 42 entries. It requires an alternate form to represent the matrix without considering the null entries.

The alternate data structure that we consider to represent a sparse matrix is a triplet. The triplet is a two dimensional array having t+1 rows and 3 columns. Where, t is total number of nonzero entries. The first row of the triplet contains number of rows, columns and nonzero entries available in the matrix in its 1st, 2nd and 3rd column respectively. Second row onwards it contains the row subscript, column subscript and the value of the nonzero entry in its 1st, 2nd and 3rd column respectively. Let us represent the above matrix in the following triplet of 6 rows and 3 columns The above triplet contains only non-zero details by reducing the space for null entries.

Application of Linked List Sparse Matrix Representation Polynomial Representation Dynamic Storage Management

Sparse Matrix Representation using linked list

struct node { int row; int col; int data; struct node. link; } struct node { int row; int col; int data; struct node *link; }*head=NULL,*curr=NULL,*p=NULL; main() m=no. of rows n=no. of columns for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); if(a[i][j]!=0)

curr->data=a[i][j]; curr->link=NULL; if(head==NULL) head=curr; curr=(struct node*)malloc(sizeof(struct node)); curr->row=i; curr->col=j; curr->data=a[i][j]; curr->link=NULL; if(head==NULL) head=curr; else { p=head; while(p->link!=NULL) p=p->link; p->link=curr; } }//end of inner for loop }//end of outer for loop

p=head; if(head==NULL) printf("\nSparse matrix empty p=head; if(head==NULL) printf("\nSparse matrix empty!\n"); else { while(p->link!=NULL) printf("%d %d %d ",p->row,p->col,p->data); p=p->link; } printf("%d %d %d ",p->row,p->col,p->data); }// end of main

Polynomial Representation We want to represent the polynomial : We will represent each term as a node containing coefficient and exponent fields, as well as a pointer to the next term. Declarations: typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; };

Application of Stack Parenthesis Matching Tower of Hanoi Problem Quick Sort Recursion

Application: Parenthesis Matching Problem: match the left and right parentheses in a character string (a*(b+c)+d) Left parentheses: position 0 and 3 Right parentheses: position 7 and 10 Left at position 0 matches with right at position 10 (a+b))*((c+d) (0,4) Right parenthesis at 5 has no matching left parenthesis (8,12) Left parenthesis at 7 has no matching right parenthesis

Parenthesis Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-1))/(m-n) Output pairs (u,v) such that the left parenthesis at position u is matched with the right parenthesis at v. (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38) How do we implement this using a stack? Scan expression from left to right When a left parenthesis is encountered, add its position to the stack When a right parenthesis is encountered, remove matching position from the stack

Example of Parenthesis Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-1))/(m-n) 1 2 1 15 21 … stack … output (2,6) (1,13) (15,19) (21,25) Do the same for (a-b)*(c+d/(e-f))/(g+h)

Application: Towers of Hanoi Read the ancient Tower of Brahma ritual n disks to be moved from tower A to tower C with the following restrictions: Move 1 disk at a time Cannot place larger disk on top of a smaller one

Let’s solve the problem for 3 disks

Towers of Hanoi (1, 2)

Towers of Hanoi (3, 4)

Towers of Hanoi (5, 6)

Towers of Hanoi (7) So, how many moves are needed for solving 3-disk Towers of Hanoi problem?  7

Time complexity for Towers of Hanoi A very elegant solution is to use recursion. The minimum number of moves required is 2n-1 Time complexity for Towers of Hanoi is Q(2n), which is exponential! Since disks are removed from each tower in a LIFO manner, each tower can be represented as a stack

Recursive Solution n > 0 gold disks to be moved from A to C using B 1 n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using C

Recursive Solution move top disk from A to C B C 1 A

Recursive Solution move top n-1 disks from B to C using A B C 1 A

Recursive Solution To move n rings from A to C using B as spare: if n is 1 just do it, otherwise... move n-1 rings from A to B using C as spare move one ring from A to C move n-1 rings from B to C using A as spare

#include <stdio. h> int n,A,B,C; / #include <stdio.h> int n,A,B,C; /* number to move, source pole, destination pole and spare pole respectively */ void move(n,A,C,B) { if (n==1) printf("Move from %d to %d.\n",A,C); } else move(n-1,A,B,C); move(1,A,C,B); move(n-1,B,C,A); main() move(4,1,3,2);