Exercise - Programming

Example 1 has given weighted, G = (V, E), wherein the edge of each negative real numbers to non-right view (shown below). Further, a back vertex set V, designated source, this question is a source vertex. Now calculate the shortest length from the source to the other all the vertices. Herein refers to the length of the road is the road edge and the respective weights. Now using Dijkstra algorithm from the source vertex to a shortest path between other vertices. Please fill in this process in the following table.

 

 

 

 

 

Iteration

S

The

Dist[2]

Dist[3]

Dist[4]

Diast[5]

initial

{1}

 

 

 

 

 

1

 

 

 

 

 

 

2

 

 

 

 

 

 

3

 

 

 

 

 

 

4

 

 

 

 

 

 

answer:

Iteration

S

The

Dist[2]

Dist[3]

Dist[4]

Dist[5]

initial

{1}

-

10

 

30

100

1

{1,2}

2

10

60

30

100

2

{1,2,4}

4

10

50

30

90

3

{1,2,4,3}

3

10

50

30

60

4

{1,2,4,3,5}

5

10

50

30

60

 

Example 2: The basic idea is quick sort: Take any one element of the n elements to be sorted (usually take the first element) as a reference, after the element into the final position, the entire data sequence is divided into two reference subsequences, all the elements is less than the reference sequence is placed in the front, all the elements are placed in the larger than the reference sequence in the reference row and two in the middle of subsequences, a process known as partitioning. Division algorithm is as follows:

int Partition (int a [], int s, int t) // partitioning algorithm position, returns after a good division of the reference element. is a column to be sorted, s is the start of a column to be sorted index, t is the end of the column to be sorted index.

{  int i=s,j=t;

  int tmp = a [s]; // with the first record as a reference sequence

  while (i! = j) // alternately from both ends to the middle scan sequence, up until i = j

  {   while (j>i && a[j]>=tmp)

      j- -; // scanned from right to left, the first to find a key is smaller than the tmp a [j]

    a [i] = a [j]; // to a [j] is moved before a [i] position

    while (i<j && a[i]<=tmp)

      i ++; // scan from left to right, looking for a key is larger than the first tmp is a [i]

    a [j] = a [i]; // the a [i] after move a [j] position

  }

  a[i]=tmp;

  return i;

}

Set to be sorted as: 7,8,6,4,10,3,9,5. Starting at index 0, the end of the standard 7. Please write the location (index value) and the reference element of a sequence of first call of the algorithm is located.

Fifth, algorithm design

Examples of materials required for all of the assignments and can be designed in conjunction with data structures, construction algorithm, for example as follows:

 

Example machine scheduling problems: there are n independent jobs {1,2, ..., n}, of m sets of the same machine {1,2, ..., m} for processing, the processing time required for the job i as ti (1≤i≤n), each job can be processed on any one machine, but not allowed to interrupt before completion of any work can not be split into smaller sub-job. Greedy algorithm to solve the problem. The following data structure is defined.

#define M 20 // largest number of machine units

#define N 10 // largest number of jobs

typedef struct // store a job using the structure

{Int w; // job number

int t; // job processing time

} Work; // Work is job type, the data type used to save a job

typedef struct // store a job using the structure

Machine job sequence //; {Work seq [N]

// total number of jobs processed by machines; int num

int sumt; // machine total treatment time

 

} PlanType; // the type of scheduling assignment scheme

Assume that each job has been processed in descending order according to time may be taken as a greedy algorithm for scheduling scheme.

void Mscheduling (Work w [], int n, PlanType S [], int m) // find the n operations w [0..n] m in the machine s [0..m] on the scheduling scheme.

{Int i, j, k; // i for controlling the dispensing operation, j is used to save the machine with minimal processing time in the machine index, k is used to control the machine changes the subject.

   for (i = 0; i <m; i ++) // m job assigned to the machine m

  { S[i].num=S[i].sumt=0;

     S [i] .seq [S [i] .num] = w [i]; // job P [i] is assigned to machine i

     S [i] .sumt = w [i] .t; // accumulated treatment time

  }

 

                                // assign the remaining work

 

           

 

                                // find all of the minimum processing time of the total number of machine subscript j

 

                                   

 

                                // job P [i] is assigned to the machine j

 

                                // accumulate processing time

 

                                // cumulative processing jobs

 

  }

}

Guess you like

Origin www.cnblogs.com/zhai1997/p/12143066.html