Arrays are data structures that store a collection of data values of the same type in consecutive memory locations that can be individually referenced by their numeric index. Different representations of arrays are possible including using a single block of memory or a collection of elements. Common operations on arrays include retrieving or storing elements by their index and traversing the entire array sequentially.
C programming, learn array 2020 week 5 and week 6, Students should know how to define/declare, initialize arrays, and multidimensional arrays types. so they can apply this knowledge during the implementation of software applications.
C programming, learn array 2020 week 5 and week 6, Students should know how to define/declare, initialize arrays, and multidimensional arrays types. so they can apply this knowledge during the implementation of software applications.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
2D array in C++ language ,define the concept of c++ Two-Dimensional array .with example .and also Accessing Array Components concept.and Processing Two-Dimensional Arrays.
Array
Introduction
One-dimensional array
Multidimensional array
Advantage of Array
Write a C program using arrays that produces the multiplication of two matrices.
2D array in C++ language ,define the concept of c++ Two-Dimensional array .with example .and also Accessing Array Components concept.and Processing Two-Dimensional Arrays.
Other than some generic containers like list, Python in its definition can also handle containers with specified data types. Array can be handled in python by module named “array“. They can be useful when we have to manipulate only a specific data type values.
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
Homework Assignment – Array Technical Document
Write a technical document that describes the structure and use of arrays. The document should
be 3 to 5 pages and include an Introduction section, giving a brief synopsis of the document and
arrays, a Body section, describing arrays and giving an annotated example of their use as a
programming construct, and a conclusion to revisit important information about arrays described
in the Body of the document. Some suggested material to include:
Declaring arrays of various types
Array pointers
Printing and processing arrays
Sorting and searching arrays
Multidimensional arrays
Indexing arrays of various dimension
Array representation in memory by data type
Passing arrays as arguments
If you find any useful images on the Internet, you can use them as long as you cite the source in
end notes.
Solution
Array is a collection of variables of the same type that are referenced by a common name.
Specific elements or variables in the array are accessed by means of index into the array.
If taking about C, In C all arrays consist of contiguous memory locations. The lowest address
corresponds to the first element in the array while the largest address corresponds to the last
element in the array.
C supports both single and multi-dimensional arrays.
1) Single Dimension Arrays:-
Syntax:- type var_name[size];
where type is the type of each element in the array, var_name is any valid identifier, and size is
the number of elements in the array which has to be a constant value.
*Array always use zero as index to first element.
The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1
For Example :- To load an array with values 0 .. 99
int x[100] ;
int i ;
for ( i = 0; i < 100; i++ )
x[i] = i ;
To determine to size of an array at run time the sizeof operator is used. This returns the size in
bytes of its argument. The name of the array is given as the operand
size_of_array = sizeof ( array_name ) ;
2) Initialisg array:-
Arrays can be initialised at time of declaration in the following manner.
type array[ size ] = { value list };
For Example :-
int i[5] = {1, 2, 3, 4, 5 } ;
i[0] = 1, i[1] = 2, etc.
The size specification in the declaration may be omitted which causes the compiler to count the
number of elements in the value list and allocate appropriate storage.
For Example :- int i[ ] = { 1, 2, 3, 4, 5 } ;
3) Multidimensional array:-
Multidimensional arrays of any dimension are possible in C but in practice only two or three
dimensional arrays are workable. The most common multidimensional array is a two
dimensional array for example the computer display, board games, a mathematical matrix etc.
Syntax :type name [ rows ] [ columns ] ;
For Example :- 2D array of dimension 2 X 3.
int d[ 2 ] [ 3 ] ;
A two dimensional array is actually an array of arrays, in the above case an array of two integer
arrays (the rows) each with three elements, and is stored row-wise in memory.
For Example :- Program to fill .
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Hivelance Technology
Cryptocurrency trading bots are computer programs designed to automate buying, selling, and managing cryptocurrency transactions. These bots utilize advanced algorithms and machine learning techniques to analyze market data, identify trading opportunities, and execute trades on behalf of their users. By automating the decision-making process, crypto trading bots can react to market changes faster than human traders
Hivelance, a leading provider of cryptocurrency trading bot development services, stands out as the premier choice for crypto traders and developers. Hivelance boasts a team of seasoned cryptocurrency experts and software engineers who deeply understand the crypto market and the latest trends in automated trading, Hivelance leverages the latest technologies and tools in the industry, including advanced AI and machine learning algorithms, to create highly efficient and adaptable crypto trading bots
Experience our free, in-depth three-part Tendenci Platform Corporate Membership Management workshop series! In Session 1 on May 14th, 2024, we began with an Introduction and Setup, mastering the configuration of your Corporate Membership Module settings to establish membership types, applications, and more. Then, on May 16th, 2024, in Session 2, we focused on binding individual members to a Corporate Membership and Corporate Reps, teaching you how to add individual members and assign Corporate Representatives to manage dues, renewals, and associated members. Finally, on May 28th, 2024, in Session 3, we covered questions and concerns, addressing any queries or issues you may have.
For more Tendenci AMS events, check out www.tendenci.com/events
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
Worried about document security while sharing them in Salesforce? Fret no more! Here are the top-notch security standards XfilesPro upholds to ensure strong security for your Salesforce documents while sharing with internal or external people.
To learn more, read the blog: https://www.xfilespro.com/how-does-xfilespro-make-document-sharing-secure-and-seamless-in-salesforce/
Globus Connect Server Deep Dive - GlobusWorld 2024Globus
We explore the Globus Connect Server (GCS) architecture and experiment with advanced configuration options and use cases. This content is targeted at system administrators who are familiar with GCS and currently operate—or are planning to operate—broader deployments at their institution.
Listen to the keynote address and hear about the latest developments from Rachana Ananthakrishnan and Ian Foster who review the updates to the Globus Platform and Service, and the relevance of Globus to the scientific community as an automation platform to accelerate scientific discovery.
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
The European Union Agency for Law Enforcement Cooperation (Europol) has suffered an alleged data breach after a notorious threat actor claimed to have exfiltrated data from its systems. Infamous data leaker IntelBroker posted on the even more infamous BreachForums hacking forum, saying that Europol suffered a data breach this month.
The alleged breach affected Europol agencies CCSE, EC3, Europol Platform for Experts, Law Enforcement Forum, and SIRIUS. Infiltration of these entities can disrupt ongoing investigations and compromise sensitive intelligence shared among international law enforcement agencies.
However, this is neither the first nor the last activity of IntekBroker. We have compiled for you what happened in the last few days. To track such hacker activities on dark web sources like hacker forums, private Telegram channels, and other hidden platforms where cyber threats often originate, you can check SOCRadar’s Dark Web News.
Stay Informed on Threat Actors’ Activity on the Dark Web with SOCRadar!
Developing Distributed High-performance Computing Capabilities of an Open Sci...Globus
COVID-19 had an unprecedented impact on scientific collaboration. The pandemic and its broad response from the scientific community has forged new relationships among public health practitioners, mathematical modelers, and scientific computing specialists, while revealing critical gaps in exploiting advanced computing systems to support urgent decision making. Informed by our team’s work in applying high-performance computing in support of public health decision makers during the COVID-19 pandemic, we present how Globus technologies are enabling the development of an open science platform for robust epidemic analysis, with the goal of collaborative, secure, distributed, on-demand, and fast time-to-solution analyses to support public health.
Cyaniclab : Software Development Agency Portfolio.pdfCyanic lab
CyanicLab, an offshore custom software development company based in Sweden,India, Finland, is your go-to partner for startup development and innovative web design solutions. Our expert team specializes in crafting cutting-edge software tailored to meet the unique needs of startups and established enterprises alike. From conceptualization to execution, we offer comprehensive services including web and mobile app development, UI/UX design, and ongoing software maintenance. Ready to elevate your business? Contact CyanicLab today and let us propel your vision to success with our top-notch IT solutions.
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Globus
Large Language Models (LLMs) are currently the center of attention in the tech world, particularly for their potential to advance research. In this presentation, we'll explore a straightforward and effective method for quickly initiating inference runs on supercomputers using the vLLM tool with Globus Compute, specifically on the Polaris system at ALCF. We'll begin by briefly discussing the popularity and applications of LLMs in various fields. Following this, we will introduce the vLLM tool, and explain how it integrates with Globus Compute to efficiently manage LLM operations on Polaris. Attendees will learn the practical aspects of setting up and remotely triggering LLMs from local machines, focusing on ease of use and efficiency. This talk is ideal for researchers and practitioners looking to leverage the power of LLMs in their work, offering a clear guide to harnessing supercomputing resources for quick and effective LLM inference.
Check out the webinar slides to learn more about how XfilesPro transforms Salesforce document management by leveraging its world-class applications. For more details, please connect with sales@xfilespro.com
If you want to watch the on-demand webinar, please click here: https://www.xfilespro.com/webinars/salesforce-document-management-2-0-smarter-faster-better/
Why React Native as a Strategic Advantage for Startup Innovation.pdfayushiqss
Do you know that React Native is being increasingly adopted by startups as well as big companies in the mobile app development industry? Big names like Facebook, Instagram, and Pinterest have already integrated this robust open-source framework.
In fact, according to a report by Statista, the number of React Native developers has been steadily increasing over the years, reaching an estimated 1.9 million by the end of 2024. This means that the demand for this framework in the job market has been growing making it a valuable skill.
But what makes React Native so popular for mobile application development? It offers excellent cross-platform capabilities among other benefits. This way, with React Native, developers can write code once and run it on both iOS and Android devices thus saving time and resources leading to shorter development cycles hence faster time-to-market for your app.
Let’s take the example of a startup, which wanted to release their app on both iOS and Android at once. Through the use of React Native they managed to create an app and bring it into the market within a very short period. This helped them gain an advantage over their competitors because they had access to a large user base who were able to generate revenue quickly for them.
We describe the deployment and use of Globus Compute for remote computation. This content is aimed at researchers who wish to compute on remote resources using a unified programming interface, as well as system administrators who will deploy and operate Globus Compute services on their research computing infrastructure.
Accelerate Enterprise Software Engineering with PlatformlessWSO2
Key takeaways:
Challenges of building platforms and the benefits of platformless.
Key principles of platformless, including API-first, cloud-native middleware, platform engineering, and developer experience.
How Choreo enables the platformless experience.
How key concepts like application architecture, domain-driven design, zero trust, and cell-based architecture are inherently a part of Choreo.
Demo of an end-to-end app built and deployed on Choreo.
Understanding Globus Data Transfers with NetSageGlobus
NetSage is an open privacy-aware network measurement, analysis, and visualization service designed to help end-users visualize and reason about large data transfers. NetSage traditionally has used a combination of passive measurements, including SNMP and flow data, as well as active measurements, mainly perfSONAR, to provide longitudinal network performance data visualization. It has been deployed by dozens of networks world wide, and is supported domestically by the Engagement and Performance Operations Center (EPOC), NSF #2328479. We have recently expanded the NetSage data sources to include logs for Globus data transfers, following the same privacy-preserving approach as for Flow data. Using the logs for the Texas Advanced Computing Center (TACC) as an example, this talk will walk through several different example use cases that NetSage can answer, including: Who is using Globus to share data with my institution, and what kind of performance are they able to achieve? How many transfers has Globus supported for us? Which sites are we sharing the most data with, and how is that changing over time? How is my site using Globus to move data internally, and what kind of performance do we see for those transfers? What percentage of data transfers at my institution used Globus, and how did the overall data transfer performance compare to the Globus users?
top nidhi software solution freedownloadvrstrong314
This presentation emphasizes the importance of data security and legal compliance for Nidhi companies in India. It highlights how online Nidhi software solutions, like Vector Nidhi Software, offer advanced features tailored to these needs. Key aspects include encryption, access controls, and audit trails to ensure data security. The software complies with regulatory guidelines from the MCA and RBI and adheres to Nidhi Rules, 2014. With customizable, user-friendly interfaces and real-time features, these Nidhi software solutions enhance efficiency, support growth, and provide exceptional member services. The presentation concludes with contact information for further inquiries.
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
Data privacy is one of the most critical issues that businesses face. This presentation shares insights on the principles and best practices for ensuring the resilience and security of your workload.
Drawing on a real-life project from the HR industry, the various challenges will be demonstrated: data protection, self-healing, business continuity, security, and transparency of data processing. This systematized approach allowed to create a secure AWS cloud infrastructure that not only met strict compliance rules but also exceeded the client's expectations.
Enhancing Research Orchestration Capabilities at ORNL.pdfGlobus
Cross-facility research orchestration comes with ever-changing constraints regarding the availability and suitability of various compute and data resources. In short, a flexible data and processing fabric is needed to enable the dynamic redirection of data and compute tasks throughout the lifecycle of an experiment. In this talk, we illustrate how we easily leveraged Globus services to instrument the ACE research testbed at the Oak Ridge Leadership Computing Facility with flexible data and task orchestration capabilities.
Quarkus Hidden and Forbidden ExtensionsMax Andersen
Quarkus has a vast extension ecosystem and is known for its subsonic and subatomic feature set. Some of these features are not as well known, and some extensions are less talked about, but that does not make them less interesting - quite the opposite.
Come join this talk to see some tips and tricks for using Quarkus and some of the lesser known features, extensions and development techniques.
2. Arrays
Array: a set of index and value
data structure
For each index, there is a value associated with
that index.
representation (possible)
implemented by using consecutive memory.
Example: int list[5]: list[0], …, list[4] each contains
an integer
0 1 2 3 4
List
3. class GeneralArray {
// objects: A set of pairs < index, value> where for each value of index in IndexSet
there is a value of type float. IndexSet is a finite ordered set of one or more
dimensions,
// for example, {0, …, n-1} for one dimension, {(0, 0), (0, 1), (0, 2),(1, 0), (1, 1), (1, 2),
(2, 0),
// (2, 1), (2, 2)} for two dimensions, etc.
public:
GeneralArray(int j; RangeList list, float initValue = defatultValue);
// The constructor GeneralArray creates a j dimensional array of floats; the range
of
// the kth dimension is given by the kth element of list. For each index i in the
index
// set, insert <i, initValue> into the array.
float Retrieve(index i);
// if (i is in the index set of the array) return the float associated with i
// in the array; else signal an error
void Store(index i, float x);
// if (i is in the index set of the array) delete any pair of the form <i, y> present
// in the array and insert the new pair <i, x>; else signal an error.
}; // end of GeneralArray
4. Arrays in C
int list[5], *plist[5];
list[5]: five integers
list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3], plist[4]
0 1 2 3 4
Plist
0 1 2 3 4
List
5. Arrays in C (cont’d)
implementation of 1-D array
list[0] base address = α
list[1] α + sizeof(int)
list[2] α + 2*sizeof(int)
list[3] α + 3*sizeof(int)
list[4] α + 4*size(int)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
6. Example: 1-dimension array addressing
int one[] = {0, 1, 2, 3, 4};
Goal: print out address and value
void print1(int *ptr, int rows)
{
/* print out a one-dimensional array using a pointer */
int i;
printf(“Address Contentsn”);
for (i=0; i < rows; i++)
printf(“%8u%5dn”, ptr+i, *(ptr+i));
printf(“n”);
}
9. Create structure data type
typedef struct human_being {
char name[10];
int age;
float salary;
};
or
typedef struct {
char name[10];
int age;
float salary
} human_being;
human_being person1, person2;
10. Unions
Similar to struct, but only one field is active.
Example: Add fields for male and female.
typedef struct sex_type {
enum tag_field {female, male} sex;
union {
int children;
int beard;
} u;
};
typedef struct human_being {
char name[10];
int age;
float salary;
date dob;
sex_type sex_info;
}
human_being person1, person2;
person1.sex_info.sex=male;
person1.sex_info.u.beard=FALSE;
11. Self-Referential Structures
One or more of its components is a pointer to itself.
typedef struct list {
char data;
list *link;
}
list item1, item2, item3;
item1.data=‘a’;
item2.data=‘b’;
item3.data=‘c’;
item1.link=item2.link=item3.link=NULL;
malloc: obtain a node
Construct a list with three nodes
item1.link=&item2;
item2.link=&item3;
a b c
13. Operations on Ordered List
(1) Find the length, n , of the list.
(2) Read the items from left to right (or right to left).
(3) Retrieve the i’th element.
(4) Store a new value into the i’th position.
(5) Insert a new element at the position i , causing
elements numbered i, i+1, …, n to become numbered
i+1, i+2, …, n+1
(6) Delete the element at position i , causing elements
numbered i+1, …, n to become numbered i, i+1, …,
n-1 array (sequential mapping)? (1)~(4) O (5)~(6) X
14. ne
n
e
xaxaxp ++= ...)( 1
1
Polynomials A(X)=3X20+2X5+4, B(X)=X4+10X3+3X2+1
class polynomial {
// objects: a set of ordered pairs of <ei, ai>
//where ai Coefficient and ei Exponent
// We assume that Exponent consists of integers ≥ 0
public:
Polynomial();
// return the polynomial p(x) = 0
int operator!();
// if *this is the zero polynomial, return 1; else return 0;
Coefficient Coef(Exponent e);
// return the coefficient of e in *this
Exponent LeadExp();
// return the largest exponent in *this
Polynomial Add(Polynomial poly);
// return the sum of the polynomials *this and poly
Polynomial Mult(Polynomial poly);
// return the product of the polynomials *this and poly
float Eval(float f);
// Evaluate the polynomial *this at f and return the result
}; end of Polynomial
16. /* d =a + b, where a, b, and d are polynomials */
d = Zero( )
while (! IsZero(a) && ! IsZero(b)) do {
switch COMPARE (Lead_Exp(a), Lead_Exp(b)) {
case -1: d =
Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b));
b = Remove(b, Lead_Exp(b));
break;
case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b));
if (sum) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
}
break;
case 1: d =
Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}
}
insert any remaining terms of a or b into d
Polynomial Addition
17. use one global array to store all polynomials
A.Start A.Finish B.Start B.Finish free
coef 2 1 1 10 3 1
exp 1000 0 4 3 2 0
0 1 2 3 4 5 6
A(X)=2X1000+1
B(X)=X4+10X3+3X2+1
specification representation
poly <start, finish>
A <0,1>
B <2,5>
Polynomial Representation 3
18. storage requirements: start, finish, 2*(finish-start+1)
nonsparse: twice as much as (1)
when all the items are nonzero
class term {
friend Polynomial;
private:
float coef; // coefficient
int exp; // exponent
};
class Polynomial; // forward delcaration
{ private:
static term termArray[MaxTerms];
static int free;
int Start, Finish;
}
term Polynomial:: termArray[MaxTerms];
int Polynomial::free = 0; // location of next free location in temArray
19. Polynomial Polynomial:: Add(Polynomial B)
// return the sum of A(x) ( in *this) and B(x)
{
Polynomial C; int a = Start; int b = B.Start; C.Start = free; float c;
while ((a <= Finish) && (b <= B.Finish))
switch (compare(termArray[a].exp, termArray[b].exp)) {
case ‘=‘:
c = termArray[a].coef +termArray[b].coef;
if ( c ) NewTerm(c, termArray[a].exp);
a++; b++;
break;
case ‘<‘:
NewTerm(termArray[b].coef, termArray[b].exp);
b++;
case ‘>’:
NewTerm(termArray[a].coef, termArray[a].exp);
a++;
} // end of switch and while
// add in remaining terms of A(x)
for (; a<= Finish; a++)
NewTerm(termArray[a].coef, termArray[a].exp);
// add in remaining terms of B(x)
for (; b<= B.Finish; b++)
NewTerm(termArray[b].coef, termArray[b].exp);
C.Finish = free – 1;
return C;
} // end of Add
Analysis: O(n+m) where n (m) is
the number of nonzeros in A(B).
20. Adding a new Term
void Polynomial::NewTerm(float c, int e)
// Add a new term to C(x)
{
if (free >= MaxTerms) {
cerr << “Too many terms in polynomials”<< endl;
exit();
}
termArray[free].coef = c;
termArray[free].exp = e;
free++;
} // end of NewTerm
21. Disadvantages of Representing Polynomials
by Arrays
What should we do when free is going to exceed
MaxTerms?
– Either quit or reused the space of unused polynomials.
But costly.
If use a single array of terms for each polynomial,
it may alleviate the above issue but it penalizes
the performance of the program due to the need of
knowing the size of a polynomial beforehand.
23. class SparseMatrix
// objects: A set of triples, <row, column, value>, where row and
column are integers and form a unique combinations; value is also
an integer.
public:
SparseMatrix(int MaxRow, int MaxCol);
// the constructor function creates a SparseMatrix that can
hold up to
// MaxInterms = MaxRow x MaxCol and whose maximum
row size is MaxRow
// and whose maximum column size is MaxCol
SparseMatrix Transpose();
// returns the SparseMatrix obtained by interchanging the
row and column value
// of every triple in *this
SPARSE MATRIX ABSTRACT DATA TYPE
24. SparseMatrix Add(SparseMatrix b);
// if the dimensions of a (*this) and b are the same, then the matrix
produced by adding corresponding items, namely those with identical
row and column values is returned
// else error.
SparseMatrix Multiply(SparseMatrix b);
// if number of columns in a (*this) equals number of rows in b then
the matrix d produced by multiplying a by b according to the formula
d[i][j] = Σ(a[i][k] . b[k][j]), where d[i][j] is the (i, j)th element, is returned.
k ranges from 0 to the number of columns in a – 1
// else error
25. Sparse Matrix Representation
Use triple <row, column, value>
Store triples row by row
For all triples within a row, their column
indices are in ascending order.
Must know the number of rows and
columns and the number of nonzero
elements
26. row col value row col value
a[0] 0 0 15 b[0] 0 0 15
[1] 0 3 22 [1] 0 4 91
[2] 0 5 -15 [2] 1 1 11
[3] 1 1 11 [3] 2 1 3
[4] 1 2 3 [4] 2 5 28
[5] 2 3 -6 [5] 3 0 22
[6] 4 0 91 [6] 3 2 -6
[7] 5 2 28 [7] 5 0 -15
(a) (b)
(1) Represented by a two-dimensional array.
Sparse matrix wastes space.
(2) Each element is characterized by <row, col, value>.
row, column in ascending order
transpose
27. class SparseMatrix; // forward declaration
class MatrixTerm {
friend class SparseMatrix
private:
int row, col, value;
};
In class SparseMatrix:
private:
int Rows, Cols, Terms;
MatrixTerm smArray[MaxTerms];
28. Transpose a Matrix
(1) for each row i
take element <i, j, value> and store it
in element <j, i, value> of the transpose.
difficulty: where to put <j, i, value>
(0, 0, 15) ====> (0, 0, 15)
(0, 3, 22) ====> (3, 0, 22)
(0, 5, -15) ====> (5, 0, -15)
(1, 1, 11) ====> (1, 1, 11)
Move elements down very often.
(2) For all elements in column j,
place element <i, j, value> in element <j, i, value>
row
29. SparseMatrix SparseMatrix::Transpose()
// return the transpose of a (*this)
{
SparseMatrix b;
b.Rows = Cols; // rows in b = columns in a
b.Cols = Rows; // columns in b = rows in a
b.Terms = Terms; // terms in b = terms in a
if (Terms > 0) // nonzero matrix
{
int CurrentB = 0;
for (int c = 0; c < Cols; c++) // transpose by columns
for (int i = 0; i < Terms; i++)
// find elements in column c
if (smArray[i].col == c) {
b.smArray[CurrentB].row = c;
b.smArray[CurrentB].col = smArray[i].row;
b.smArray[CurrentB].value = smArray[i].value;
CurrentB++;
}
} // end of if (Terms > 0)
return b;
} // end of transpose
Time Complexity O(terms*cols)
column
term element
matrix
0
c = column
b
column assignment
30. Discussion: compared with 2-D array representation
O(columns*terms) vs. O(columns*rows)
terms--> columns * rows when nonsparse
O(columns*columns*rows)
Problem: Scan the array “columns” times.
Solution:
Determine the number of elements in each column of
the original matrix.
Determine the starting positions of each row in the
transpose matrix.
32. Fast Matrix Transposing
SparseMatrix SparseMatrix::Transpose()
// The transpose of a(*this) is placed in b and is found in Q(terms + columns) time.
{
int *Rows = new int[Cols];
int *RowStart = new int[Cols];
SparseMatrix b;
b.Rows = Cols; b.Cols = Rows; b.Terms = Terms;
int i;
if (Terms > 0) // nonzero matrix
{
// compute RowSize[i] = number of terms in row i of b
for ( i = 0; i < Cols; i++) RowSize[i] = 0; // Initialize
for ( i = 0; i < Terms; I++) RowSize[smArray[i].col]++;
// RowStart[i] = starting position of row i in b
RowStart[0] = 0;
for (i = 1; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1];
O(columns)
O(terms)
O(columns-1)
row size +1
start size
33. Fast Matrix Transposing (cont’d)
for (i =0; i < Terms; i++) // move from a to b
{
int j = RowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
RowStart[smArray[i].col]++;
} // end of for
} // end of if
delete [] RowSize;
delete [] RowStart;
return b;
} // end of FastTranspose
O(terms)
O(row * column)
Text
row
row start
row start
34. Matrix Multiplication
Definition: Given A and B, where A is mxn and B
is nxp, the product matrix Result has dimension
mxp. Its [i][j] element is
for 0 ≤ i < m and 0 ≤ j < p.
kj
n
k
ikij baresult ∑
−
=
=
1
0
35. Representation of Arrays
Multidimensional arrays are usually
implemented by one dimensional array via
either row major order or column major
order.
Example: One dimensional array
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
α α+1 α+2 α+3 α+4 α+5 α+6 α+7 α+8 α+9 α+10 α+12
36. Two Dimensional Array Row Major
Order
X X X X
X X X X
X X X X
Col 0 Col 1 Col 2 Col u2 - 1
Row 0
Row 1
Row u1 - 1
u2
elements
u2
elements
Row 0 Row 1 Row u1 - 1Row i
i * u2 element
row
37. Generalizing Array Representation
The address indexing of Array A[i1][i2],…,[in] is
α+ i1 u2 u3 … un
+ i2 u3 u4 … un
+ i3 u4 u5 … un
:
:
+ in-1 un
+ in
=α+
=
≤≤= ∏∑ +=
=
1
1
1
1
n
n
jk
kj
n
j
jj
a
njua
whereai
38. String
Usually string is represented as a
character array.
General string operations include
comparison, string concatenation, copy,
insertion, string matching, printing, etc.
H e l l o W o r l d 0
39. String Matching: Straightforward
solution
Algorithm: Simple string matching
Input: P and T, the pattern and text strings; m, the
length of P. The pattern is assumed to be nonempty.
Output: The return value is the index in T where a
copy of P begins, or -1 if no match for P is found.
•Worst-case complexity is in θ(mn)
p: pattern query string
p shift
40. Two Phases of KMP
Phase 1:generate an array to indicate the
moving direction.
Phase 2:make use of the array to move and
match string
phase 1 array
//
shift
shift
phase 2 做matching
45. String Matching The Knuth-Morris-Pratt
Algorithm
Definition: If p = p0p1…pn-1 is a pattern, then its
failure function, f, is defined as
−
≥=<
= +−−
.otherwise1
exists0asuchif......thatsuchlargest
)( 110
kppppppjk
jf jkjkjk
Failure Function
k
P
j
k
k
P
j
46. The prefix (failure) function, Π
Following pseudocode computes the prefix (i.e., failure) fucnction, Π:
Compute-Prefix-Function (p)
1 m length[p] //’p’ pattern to be matched
2 Π[1] 0
3 k 0
4 for q 2 to m
5 do while k > 0 and p[k+1] != p[q]
6 do k Π[k]
7 If p[k+1] = p[q]
8 then k k +1
9 Π[q] k
10 return Π
47. Example: compute Π for the pattern ‘p’ below:
p a b a b a c a
Initially: m = length[p] = 7
Π[1] = 0
k = 0
Step 1: q = 2, k=0
Π[2] = 0
Step 2: q = 3, k = 0,
Π[3] = 1
Step 3: q = 4, k = 1
Π[4] = 2
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1
q 1 2 3 4 5 6 7
p a b a b a c A
Π 0 0 1 2
48. Step 4: q = 5, k =2
Π[5] = 3
Step 5: q = 6, k = 3
Π[6] = 1
Step 6: q = 7, k = 1
Π[7] = 1
After iterating 6 times, the prefix
function computation is
complete:
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3 0
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3 0 1
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3 0 1
a =
49. The KMP Alogrithm (cont’d)
jj-1
S
P
Action
k
i
If a partial match is found such that si-j … si-1 =
p0p1…pj-1 and si ≠ pj then matching may be resumed by
comparing si and pf(j–1)+1 if j ≠ 0. If j = 0, we may
continue by comparing si+1 and p0.
fail function pattern array
element = element
f(j-1) +1
f(j-1)
f(j-1) + 1
50. The KMP Matcher
The KMP Matcher, with pattern ‘p’, string ‘S’ and prefix function ‘Π’ as input, finds a match of p in S.
Following pseudocode computes the matching component of KMP algorithm:
KMP-Matcher(S,p)
1 n length[S]
2 m length[p]
3 Π Compute-Prefix-Function(p)
4 q 0 //number of characters matched
5 for i 1 to n //scan S from left to right
6 do while q > 0 and p[q+1] != S[i]
7 do q Π[q] //next character does not match
8 if p[q+1] = S[i]
9 then q q + 1 //next character matches
10 if q = m //is all of p matched?
11 then print “Pattern occurs with shift” i – m
12 q Π[ q] // look for the next match
Note: KMP finds every occurrence of a ‘p’ in ‘S’. That is why KMP does not terminate in step 12,
rather it searches remainder of ‘S’ for any more occurrences of ‘p’.
51. Illustration: given a String ‘S’ and pattern ‘p’ as
follows:
S b a c b a b a b a b a c a c a
p a b a b a c a
Let us execute the KMP algorithm to find
whether ‘p’ occurs in ‘S’.
For ‘p’ the prefix function, Π was computed previously and is as follows:
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3 0 1
phase 1 pattern fail function value
52. b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
a b a b a c a
Initially: n = size of S = 15;
m = size of p = 7
Step 1: i = 1, q = 0
comparing p[1] with S[1]
S
p
P[1] does not match with S[1]. ‘p’ will be shifted one position to the right.
S
p a b a b a c a
Step 2: i = 2, q = 0
comparing p[1] with S[2]
P[1] matches S[2]. Since there is a match, p is not shifted.
53. Step 3: i = 3, q = 1
b a c b a b a b a b a c a a b
Comparing p[2] with S[3]
S
a b a b a c a
b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
a b a b a c a
a b a b a c ap
S
p
S
p
p[2] does not match with S[3]
Backtracking on p, comparing p[1] and S[3] (q= Π[1]+1=0+1=1)
Step 4: i = 4, q = 0
comparing p[1] with S[4] p[1] does not match with S[4]
Step 5: i = 5, q = 0
comparing p[1] with S[5] p[1] matches with S[5]
54. b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
a b a b a c a
a b a b a c a
a b a b a c a
Step 6: i = 6, q = 1
S
p
Comparing p[2] with S[6] p[2] matches with S[6]
S
p
Step 7: i = 7, q = 2
Comparing p[3] with S[7] p[3] matches with S[7]
Step 8: i = 8, q = 3
Comparing p[4] with S[8] p[4] matches with S[8]
S
p
55. Step 9: i = 9, q = 4
Comparing p[5] with S[9]
Comparing p[6] with S[10]
Comparing p[5] with S[11]
Step 10: i = 10, q = 5
Step 11: i = 11, q = 4
S
S
S
p
p
p
b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
a b a b a c a
a b a b a c a
a b a b a c a
p[6] doesn’t match with S[10]
Backtracking on p, comparing p[4] with S[10] because after mismatch q = (Π[5]+1) = 3+1=4
p[5] matches with S[9]
p[5] matches with S[11]
看a fail function = 3
從3+1 開始比
56. b a c b a b a b a b a c a a b
b a c b a b a b a b a c a a b
a b a b a c a
a b a b a c a
Step 12: i = 12, q = 5
Comparing p[6] with S[12]
Comparing p[7] with S[13]
S
S
p
p
Step 13: i = 13, q = 6
p[6] matches with S[12]
p[7] matches with S[13]
Pattern ‘p’ has been found to completely occur in string ‘S’.
57. The analysis of the K.M.P. Algorithm
O(m+n)
– O(m) for computing function f
– O(n) for searching P