• Keine Ergebnisse gefunden

Graphs and Elementary Algorithms

N/A
N/A
Protected

Academic year: 2022

Aktie "Graphs and Elementary Algorithms"

Copied!
5
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Graphs and Elementary Algorithms

You are to develop a C library which represents graphs in the forms of adjaceny matrices and adjacency lists. The following are to implemented:

• Creation and destruction of a graph

• Insertion of nodes and edges to a graph

• Counting the number of edges in a graph

• Printing the graph to the command line

• Persistency (File operations)

• Depth-first search and breadth-first search

• Checking reachability of nodes in a graph

• Finding the shortest path The project phases are as follows.

1. Introduction to the Tasks

2. Test-First Implementation of Function Prototypes

• Informal Definition of Test Objectives

• Setting up the Test Cases and their Implementation 3. Stepwise for Functions

• Implmentation

• Documentation of Source Code

• Test Execution 4. Final Documentation

• C0-Test using Gcov, Plug-in for Eclipse

http://sourceforge.net/projects/gcov-eclipse/

• Document Generation using Doxygen (http://www.doxygen.org)

Each student shall be responsible for a minimum of six functions (test, implementation, documentation). The name of the person who is responsible for a function shall be documented in the source code using Doxygen command \author.

(2)

1. A FIFO list shall be implemented. You will need this later for implementation of breadth- first search. The necessary data types for the list and its elements are already given in FiFoList.h. In addition, the prototypes for the functions already exist in this file.

• void *dequeue(FiFoList *pList);

Removes the first node of the list and returns a pointer to the user data in this node.

• void enqueue(FiFoList *pList, void *pData);

Inserts a node containing the user data at the end of the list using.

• bool isEmpty(FiFoList *pList);

Checks whether the list is empty.

Hint:

• The implementation of these three functions will be placed into FiFoList.c.

• The data typeListElementcontains the declaration void *pData, that is, a typeless pointer, which points to the actual user data. The advantage of this usage is that the list is not fixed to work with a certain type of data such as int. Therefore, it is usable with any type of data. However, proper care needs to be taken to allocate memory for the user data (before calling enqueue) and later to free it (after calling dequeue).

By casting a pointer appropriately, the pointer is not limited only to a certain type of data; it can be cast again to another type.

2. In AdjacencyMatrix.h, you will find the data type adjacencyMatrix for representation of an adjacency matrix, in addition to different prototypes for manipulation of adjacency matrix. First, you shall implement the function createAdjMatrix.

This shall work as follows.

(a) Allocation of dynamic memory for an adjacencyMatrix (b) Setting the number of nodes

(c) Construction of the matrix with dynamic memory allocation (⇒ppMatrix) (d) Initialization of matrix entries

You shall also implement the function freeAdjMatrix to free the memory dynamically allocated for an adjacency matrix.

3. You shall implement two functions which inserts a new node and a new edge to an adjacency matrix. The prototypes are already given asaddEdgeToAdjMatrixandaddNodeToAdjMatrix.

Hint: An edge (a, b) is inserted into the posititon ppMatrix[a][b] in the matrix. The tutorial explains how a single-dimensional array can be extended dynamically. The exten- sion of an n×n matrix is schematically explained with an illustration. However, it works similarly.

4. The functioncountEdgesInAdjMatrixshall be implemented to count the number of edges in an adjacency matrix and printAdjMatrix to print the matrix to the console.

(3)

5. Two functions shall be implemented to write an adjacency matrix to a file and read it from a file. The prototypes are readAdjMatrixFromFile and writeAdjMatrixToFile.

An adjacency matrix in a file has the following form.

• The first line contains the number of nodes.

• Each following line corresponds to a row of the matrix.

• The entries in a row are separated by spaces.

Tabel 1 contains an example graph with 5 nodes and 6 edges.

Table 1: File format for an adjacency matrix 5

0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1

6. You shall implement the functiondepthFirstSearch, which performs a depth-first search in an adjacency matrix recursively.

Hint: Tutorial contains a pseudocode for the implementation of the depth-first search.

7. In AdjacencyList.h, you will find the data types nodeand adjacencyList for representa- tion of an adjacency list. Figure 1 shows how an adjacency list is realized.

Figure 1: Adjacency List

You shall implement the following functions for the manipulation of an adjacency list.

adjacencyList *createAdjList(int numberOfNodes);

void freeAdjList(adjacencyList *pAdjList);

void addEdgeToAdjList(adjacencyList *pAdjList, int startnode, int endnode);

int addNodeToAdjList(adjacencyList *pAdjList);

int countEdgesInAdjList(adjacencyList *pAdjList);

(4)

void printAdjList(adjacencyList *pAdjList);

Hint: Use the file AdjacencyList.c for implementation.

8. You shall implement breadth-first search which works on an adjacency list using the pro- totype breadthFirstSearch.

Hint: Tutorial contains a pseudocode for the implementation.

9. The file tools.h contains the prototypescreateAdjMatrixFromList and

createAdjListFromMatrix. You shall implement these functions to convert an adjacency list to an adjacency matrix, and vice versa.

10. There are two functions in tools.c which use depth-first search and breadth-first search to check whether a node is reachable from all the other nodes in a given graph. The func- tion checkReachabilityMatrix shall make use of the depth-first search and the function checkReachabilityList the breadth-first search.

The file Graph.zip (also Graph.txt) contains a graph with 1000 nodes. You shall perform the reachability check of the node with index 0 using both functions.

11. You shall implement the functionpathCheck in tools.c to check whether there exist a path from a tob wherea and b are two given nodes.

12. You shall implement the functionfindShortestPathin tools.c. Given two nodes a and b, this function computes the shortest path from atob and prints it to the console, if there is such a path. To find the path, you can use the breadth-first search under the assumption that each edge of the graph has the weight of 1.

13. You shall convert the graph in Graph.txt using the function writeGraphMLFile into GraphML format. UsingyEd(http://www.yworks.com/en/products_yed_about.html), you shall open the new file using and try different layouts for illustration of the graph.

(5)

Programming Convention

• In the beginning of each C file, there should a comment part which has the following form.

/***********************************************

* File: filename.c *

* Group X: Axel Hollmann 7654321 *

* Michael Linschulte 1212121 *

* Sascha Padberg 1234567 *

* Date: 30.03.2010 *

* Description: Description of the file *

***********************************************/

• Identifiers

– The names should be self-explanatory (for example, considering the use of a variable) and in English.

– The names should start with small letters and contain at least 3 letters (Exceptions:

Index variables such as i, k, l, n).

– In variable names which contain multiple words, the words should not be separated but each word except for the first one should start with a capital letter.

Example: int positionX, nextValue;

– Pointer variables: For each star, there should be a p at the begining of the variable name.

(Example: int *pInt; char **ppMatrix;)

• Macros are composed of only capital letters.

• Comments

The source code should be commented carefully. The prototypes in the header files should be documented using Doxygen.

• Identation

Identations contain 3 (or 4) spaces. Curly brackets, which belong together, should be in the same column.

if(...) {

...

}

Referenzen

ÄHNLICHE DOKUMENTE

It has been shown that in kinetically first order gas phase reactions occuring under electric discharge, such as the de- composition of N 2 0 , the application, at various

I recognize duplicates: when a state is reached on multiple paths, only keep one search node I search nodes correspond 1:1 to reachable states I search tree bounded, as number of

I breadth-first search ( this chapter) I uniform cost search ( Chapter 11) I depth-first search ( Chapter 12) I depth-limited search ( Chapter 12) I iterative deepening search (

Theorem (time complextive of iterative deepening DFS) Let b be the branching factor and d be the minimal solution length of the given state space. State-Space Search: Depth-first

15.1 Introduction 15.2 Best-first Search 15.3 Algorithm Details 15.4 Reopening..

In our previous hash-based DDD algorithm (Korf 2004), all parent files at a given depth being expanded before any child files at the next depth were merged.. The disadvantage of

Wiederhole, solange die Queue nicht leer ist:.. x

In our German data, after the acceptance of a possible pre-closing (extract 1, line 30: "gut."), the next activity is not a terminal greeting.. In fact, we have not found