Top Banner
May 2012 Sorting Networks Slide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering
20

May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

Dec 18, 2015

Download

Documents

Berenice Dixon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 1

Sorting NetworksA Lecture in CE Freshman Seminar Series:

Ten Puzzling Problems in Computer Engineering

Page 2: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 2

About This Presentation

This presentation belongs to the lecture series entitled “Ten Puzzling Problems in Computer Engineering,” devised for a ten-week, one-unit, freshman seminar course by Behrooz Parhami, Professor of Computer Engineering at University of California, Santa Barbara. The material can be used freely in teaching and other educational settings. Unauthorized uses, including any use for financial gain, are prohibited. © Behrooz Parhami

Edition Released Revised Revised Revised Revised

First May 2007 May 2008 May 2009 May 2010 May 2011

May 2012

Page 3: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 3

Railroad Tracks and Switches

Page 4: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 4

Coupling and Decoupling of Train Cars

Train cars and engines can be coupled and decoupled quickly

An engine can push a string of cars, or pull a desired subset by decoupling them from the rest

Page 5: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 5

Railroad Yards Have Many Tracks and Switches

Page 6: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 6

Rearranging Trains

B AD CSorted order

B A D C

Stub or lead

Stack or LIFO data structure in CE

Sorting algorithm: Assemble train in stub, beginning with the last carRepeat: If the next car is X, decouple train after X, push X into stub

BA DCSidingQueue or FIFO

B A D C TrackTrain cars Engine

B A D C

Stub or lead

B A

Stub or lead D C

Page 7: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 7

Goleta Amtrak Stationand Its Siding

Page 8: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 8

Model Railroad YardModel Railroad Yard

Page 9: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 9

Rearrangement with Change of Direction

B A D C

Turnaround loopBA DC

What types of sorting are possible with a turnaround loop? A wye?

BA DC

Wye

B

A

D

C

Configuration after the first few steps in sorting the train

Complete the sorting process, assuming that it is okay for the engine to end up behind the train

Page 10: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 10

Model Train TurntableModel Train Turntable

Page 11: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 11

Delivering Train Cars in a Specific Order

1

B A DC

2

3

Cars in the train below have been sorted according to their delivery points. However, it is still nontrivial to deposit car A in stub 1, car B in stub 2, and car C in siding 3. Cars can be pulled or pushed by the engine.

1

C

2

3

B A D

1

C

2

3

B AD

Is there a better initial ordering of the cars for the deliveries in this puzzle?

Page 12: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 12

Train Passing PuzzleThe trains below must pass each other using a siding that can hold only one car or one engine. Show how this can be done.

B A 21

B A 21

B 21

A

B 21

A

21

AB

2 A

B

1

Page 13: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 13

Fast Combining or Reordering of Train Cars

Forming multiple trains from incoming cars

BA DC

BA DC

Train reordering via a set of stubs

Cars are pushed or pulled by an engine

Alternatively, movement in one direction may be achieved via sloped rails

Switches used to be adjusted manually, but nowadays, electronic control is used

Page 14: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 14

2

4

3

1

Track 1

Track 2

Track 3

Track 4

Sorting Train Cars in Parallel

3

1

2

4

3

1

2

4 3

1

2

4

Compare-exchangeUnconditional exchangeTarget track

The net in stick diagram schematic

2

4

3

1

Track 1

Track 2

Track 3

Track 43

1

2

4 3

1

2

4

Is adding this compare-exchange element sufficient for producing a valid sorting network?

Page 15: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 15

Validating a Sorting Network

Stick diagram schematic

b

a

d

c

Track 1

Track 2

Track 3

Track 4

min(a, b)

max(a, b)

min(c, d)

max(c, d)

min(a, b, c, d)

max(a, b, c, d)

????

????

In the example above, it was fairly easy to show the validity of the sorting network. Generally, it is much more difficult

How would one establish the validity of this 16-input sorting network?

More importantly, how does one come up with this design in the first place?

Page 16: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 16

The Zero-One Principle

0000

0000

0001

0001

0010

0001

0011

0011

0100

0001

0101

0011

0110

0011

0111

0111

1000

0001

1001

0011

1010

0011

1011

0111

1100

0011

1101

0111

1110

0111

1111

1111

A sorting net built of comparators is valid if it correctly sorts all 0-1 sequences

So, we can validate a sorting network using 2n rather than n! input patterns

n = 12: 2n = 4096, n! = 479,001,600 (thousands vs. half a billion)

Page 17: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 17

A 16-Input Sorting Network

5108

12

61427

41591

111330

Use 4-input sorters, follow by (4, 4)-mergers, and end with an (8, 8)-merger

Using the 0-1 principle, we can validate this network via 16 + 25 + 81 tests

0123

4567

89

1011

12131415

58

1012

267

14

149

15

03

1113

2567

8101214

0134

9111315

4-sorter tests (4, 4)-merger tests (8, 8)-merger tests

Page 18: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 18

Insertion Sort and Selection Sort

Fig. 7.8 Sorting network based on insertion sort or selection sort.

xxx

x

.

.

.

(n–1)-sorter

0

1

2

n–2

yyy

y

0

1

2

n–2

x n–1

.

.

.

y n–1

xxx

x

.

.

.

(n–1)-sorter

0

1

2

n–2

yyy

y

0

1

2

n–2

x n–1

.

.

.

y n–1

.

.

.

Insert ion sort Selection sort

Parallel insertion sort = Parallel selection sort = Parallel bubble sort!

C(n) = n(n – 1)/2D(n ) = 2n – 3 Cost Delay = (n3)

Insertion sort Selection sort

340521

340

3045

03452

034251

032415

02314

0213

012

012345

Page 19: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 19

The Best Sorting Networks

n = 6, 12 modules, 5 levels

n = 9, 25 modules, 8 levelsn = 10, 31 modules, 7 levels

n = 12, 40 modules, 8 levels

n = 16, 61 modules, 9 levels

n = 9, 25 modules, 9 levelsn = 10, 29 modules, 9 levels

n = 12, 39 modules, 9 levels

n = 16, 60 modules, 10 levels

Cost Delay = 29 9 = 261 Cost Delay = 31 7 = 217

The most cost-effective n-input sorting network may be neither the fastest design, nor the lowest-cost design

Criterion 1: The number of sticks or compare-exchange blocks (cost)

Criterion 2: The number of compare-exchanges in sequence (delay)

Criterion 3: The product of cost and delay (cost-effectiveness)

Which 10-input sorting network is better?

Cost = 29 Cost = 31Delay = 9 Delay = 7

Page 20: May 2012Sorting NetworksSlide 1 Sorting Networks A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering.

May 2012 Sorting Networks Slide 20

Electronic Sorting NetworksElectronic sorting networks are built of 2-sorters, building blocks that can sort two inputs

Applications of sorting networks:Directing information packets to their destinations in a network routerConnecting n processors to n memory modules in a parallel computer

x0x1

x

x3

2

y0

y1

y

y3

2

2

3

1

5

3

2

5

1

1

3

2

5

1

2

3

5

a

b

min(a, b)

max(a, b)