代做CSE 101 Introduction to Data Structures and Algorithms Programming Assignment 3代写C/C++程序

CSE 101

Introduction to Data Structures and Algorithms

Programming Assignment 3

In this assignment you will build a Graph module in C that implements the Depth First Search (DFS) algorithm. You will use your Graph module to find the strongly connected components of a digraph.  Read the handout on graph algorithms, and sections 22.3-22.5 of the text.  Also see the pseudo-code posted on the class webpage at Examples/Pseudo-Code/GraphAlgorithms.

A digraph G = (V, E) is said to be strongly connected iff for every pair of vertices u, v ∈ V, vertex u is reachable from v, and vertex v is reachable from u.  Most directed graphs are not strongly connected.  In general, we say a subset X ⊆ V is strongly connected iff every vertex in X is reachable from every other vertex in X.  A strongly connected subset that is maximal with respect to this property is called a strongly connected component of G.  In other words, X  ⊆ V isa strongly connected component of G, if and only if (i) X is strongly connected, and (ii) the addition of one more vertex to X would create a subset that is not strongly connected.

Example

We can see that this digraph contains 4 strongly connected components:   C1   = {1, 2, 5}, C2   = {3, 4}, C3  = {6, 7}, and C4   = {8}.

To find the strong components of a digraph G call DFS(G).  As vertices are finished, place them onto a stack.  When DFS is complete the stack will store the vertices ordered by decreasing finish times.  Next, compute the transpose GT  of G.  (The digraph GT   is obtained by reversing directions on all edges of G.) Finally run DFS(GT ), but in the main loop (lines 5-7) of DFS, process vertices in order of decreasing finish times from the first call to DFS.  This is accomplished by popping vertices off the stack.  When the whole process is complete, the trees in the resulting DFS forest span the strong components of G. Note the strongly connected components of G are identical to the strong components of GT .   See the algorithm  Strongly- Connected-Components in section 22.5 (p.617) of the text.

Your graph module will again use the adjacency list representation.   It will, among other things, provide the capability of running DFS, and computing the transpose of a directed graph.  DFS requires that vertices possess attributes for color (white, black, grey), discover time, finish time, and parent.  Below is a catalog of required functions and prototypes, constituting the majority of Graph.h.

// Constructors-Destructors

Graph newGraph (int n);

void freeGraph (Graph* pG);

// Access functions

int getOrder(Graph G);

int getSize(Graph G);

int getParent(Graph G, int u);  /* Pre: 1<=u<=n=getOrder(G) */

int getDiscover(Graph G, int u);   /* Pre: 1<=u<=n=getOrder(G) */

int getFinish(Graph G, int u);   /* Pre: 1<=u<=n=getOrder(G) */

// Manipulation procedures

void addArc (Graph G, int u, int v);  /* Pre: 1<=u<=n, 1<=v<=n */

void addEdge(Graph G, int u, int v);  /* Pre: 1<=u<=n, 1<=v<=n */

void DFS(Graph G, List S);    /* Pre: length (S)==getOrder(G) */

// Other Functions

Graph transpose (Graph G);

Graph copyGraph(Graph G);

void printGraph(FILE* out , Graph G);

Function newGraph() will return a reference to a new graph object containing n vertices and no edges. freeGraph() frees all heap memory associated with a graph and sets its Graph argument to NULL. Function getOrder() returns the number of vertices in G, while functions getParent(), getDiscover(), and getFinish() return the appropriate field values for the given vertex.  Note that the parent of a vertex may be NIL.  The discover and finish times of vertices will be undefined before DFS is called.   You must #define constant macros for NIL and UNDEF representing those values and place the definitions in the Graph.h.   The descriptions of functions addEdge() and addArc() are exactly as they were in pa2.  Note that, as in pa2, it is required that adjacency lists are always processed in increasing order by vertex label. It is the responsibility of functions addEdge() and addArc() to maintain adjacency lists in sorted order.

Function DFS() will perform. the depth first search algorithm on G.  The List argument S has two purposes in this function.  First it defines the order in which vertices are to be processed in the main loop (5-7) of DFS.   Second, when DFS  is complete, it will store the vertices by decreasing finish times (hence S is considered to be a stack). Thus S can be classified as both an input and output parameter to function DFS(). You should utilize the List module from pa1 to implement S and the adjacency lists representing G.  DFS() has two preconditions: (i) length(S) == n, and (ii) S contains some permutation of the integers {1, 2, … , n} where n = getOrder(G).  You are required to check the first precondition but not the second.

Recall DFS() calls the recursive algorithm Visit() (referred to as DFS-Visit() in the text), and uses a variable called time that is static over all recursive calls to Visit().  Observe that this function is not mentioned in the above catalog (Graph.h) and therefore is to be considered a private helper function in Graph.c.  There are at least three possible approaches to implementing Visit(). You can define Visit() as a top-level function in Graph.c and let time be a global variable whose scope is the entire file.  This option has the drawback that other functions in Graph.c have access to time and would be able to alter its value.  For this reason, global variables are generally considered to be a poor programming practice.  The second approach is to let time be a local variable in DFS(), then pass the address of time to Visit(), making it an input-output variable to Visit(). This is perhaps the simplest option, and is recommended. The third approach is to again let time be a local variable in DFS(), then nest the definition of Visit() within the definition of DFS(). Since time is local to DFS(), its scope includes the defining block for Visit() and is therefore static throughout all recursive calls to Visit(). This maybe tricky if you’re not used to nesting function definitions since there are issues of scope to deal with.  If you pick this option, first experiment with a few examples to make sure you know how it works.  Note that although nesting function definitions is not a feature of standard C, and is not supported by many compilers, it is supported by the GNU gcc compiler (see https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html.)       There's   actually    a    fourth   option    for implementing time that may be the easiest of all.  Just give Visit() its own local copy of time, then let it take the current value of time as input, and return the new value of time when it's done.  No global variable, no passing by reference and no nested function definitions.  These design decisions are left to you, and are the type of thing that should be stated in your README.md file.

Function transpose() returns a reference to  a new graph object representing the transpose of G, and copyGraph() returns a reference to a new graph that is a copy of G.  Both transpose() and copyGraph() may be considered constructors since they create new graph objects.  Function printGraph() prints the adjacency list representation of G to the file pointed to by out.  Obviously, there is much in common between the Graph module in this project and the one in pa2.  If you wish, you may simply add functionality necessary for this project to the previous one, although it is not required that you do so.   You should make note of choices such as this in your README.md file.

The client of your Graph module will be called FindComponents. It will take two command line arguments giving the names of the input and output files respectively:

$ FindComponents infile outfile

FindComponents.c will do the following:

•   Read the input file.

•   Assemble a graph object G using newGraph() and addArc().

•   Print the adjacency list representation of G to the output file.

•   Run DFS on G and GT , processing the vertices in the second call by decreasing finish times from the first call.

•   Determine the strong components of G.

•   Print the strong components of G to the output file in topologically sorted order.

After the second call to DFS(), the List parameter S can be used to determine the strong components of G, and to determine a topological  sort of those  components.   You  should trace the algorithm  Strongly- Connected-Components (p.617) on several small examples, keeping track of the List S to see how this can be done.  Input and output file formats are illustrated in the following example, which corresponds to the directed graph on the first page of this handout:

Input:                                           Output:

8                                                   Adjacency list representation of G:

1 2                                                1: 2

2 3                                                2: 3 5 6

2 5                                                  3: 4 7

2 6                                                  4: 3 8

3 4                                                5: 1 6

3 7                                                6: 7

4 3                                                7: 6 8

4 8                                                   8: 8

5 1

5 6                                                 G contains 4 strongly connected components:

6 7                                                Component 1: 1 5 2

7 6                                                 Component 2: 3 4

7 8                                                Component 3: 7 6

8 8                                                 Component 4: 8

0 0

Observe that the input file format is very similar to that of pa2.  The first line gives the number of vertices in the graph, subsequent lines specify directed edges, and input is terminated by the ‘dummy’ line 0 0.  You are required to submit the following eight files:

README.md Makefile

List.h

List.c

Graph.h

Graph.c

GraphTest.c

FindComponents.c

As usual README.md contains a catalog of submitted files and any special notes to the grader.  Makefile should be capable of making the executables GraphTest and FindComponents and should contain a clean utility that removes all binary files.   Graph.c  and  Graph.h  are  the  implementation and interface files respectively for your Graph module.   GraphTest.c will  contain your own tests of your  Graph module. FindComponents.c implements the top-level client and main program for this project.   To get full credit, your project must implement all required files and functions, compile without errors or warnings, produce correct output on our unit tests, and produce no memory leaks under valgrind.  By now everyone knows that points are deducted both for neglecting to include required files, for misspelling any filenames and for submitting additional unwanted files but let me say it anyway: do not submit binary files of any kind.

Note that FindComponents.c needs to pass a List to the function DFS(), so FindComponents.c is also a client of the List module.  In fact, any client of Graph is also a client of List just by the presence of the List parameter to DFS().  Therefore Graph.h should itself #include the file List.h.  (See the handout entitled C Header File Guidelines for more on this topic.)

A Makefile for this project will be posted on the course webpage which you may alter as you see fit.  As usual start Early and ask questions if anything is not completely clear.




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图