本次算法代写难度较大，主要实现题目给出的两个算法并实现并行计算，涉及并行多线程和负载均衡。

THIS IS NOT A GROUP PROJECT! You may talk about the project and possible solutions in general terms, but must not share code. In this project, you will be asked to implement two parallel graph matching algorithms. Your program should take a legal matrix-market matrix ﬁle as input and output the matching results.

This document is not a complete speciﬁcation of the project. You will encounter important design and implementation issues that need to be addressed in your project solution. Identifying these issues is part of the project. As a result, you need to start early, allowing time for possible revisions of your solution.

**1 Background**

### 1.1 The Graph Matching Problem

Given a graph G = (V, E), while V is the set of vertices (also called nodes) and E ⊂ |V | 2 . A matching M in G is a set of pairwise non-adjacent edges which means that no two edges share a common vertex. A vertex is matched if it is an endpoint of one of the edges in the matching. Otherwise the vertex is unmatched [3]. In Figure 1, we show three diﬀerent matchings for the same graph.

A maximum matching can be deﬁned as a matching where the total weight of the edges in the matching is maximized. In Figure 1, (c) is a maximum matching because the total weight of the edges in the matching is 7, and there could be no other matching that has total weight greater than 7 for this graph.

### 1.2 Parallel Graph Matching Algorithms

The graph matching problem is not easy to parallelize. Most existing matching algorithms such as blossom algorithm are embarrassingly sequential. Here we describe two handshaking-based algorithms [2] that are amenable to parallelization.

**1.2.1 One-Way Handshaking Matching**

Given a graph G, two vertices shake hands only when there is an edge between these two and they are the strongest neighbor of each other. We will deﬁne “strongest neighbor”. The edge that connects the two handshaking vertices is added to the matching M. We show an example of one-way handshaking matching in Figure 2.

To identify handshaking vertices, each vertex v in G extends a hand to its strongest neighbor. Here the strongest neighbor of a vertex v is the neighbor vertex that sits on the maximum-weight edge incident to v. This is a greedy algorithm that aims to maximize the total weight in the matching. If there are multiple incident edges of v that have maximum weight, the neighbor vertex that has the smallest vertex index will be chosen as the strongest neighbor. For example, in Figure 2(b), the strongest neighbor of vertex E is vertex B because it has an edge weight of 4 and a smaller index (alphabetical order) than vertex F.

For each vertex, we check if its strongest neighbor extends a hand back. If so, these two vertices are matched. Then the corresponding edge will be added to the matching. For example, in Figure 2(c), vertex B extends a hand to vertex E and vertex E also extends a hand to vertex B, so edge BE will be added to the matching.

At every pass of the process, we check all the vertices that are not matched from previous passes (each vertex is checked once in each pass), and identify if there is any new edge that can be added to the matching. We repeat this until no more edges can be added to the matching. We show the passes in Figure 2(b), (c), (d),(e) and (f). The handshaking method is highly data parallel, since each vertex can be processed independently and there is no data race.

**1.2.2 N-Way Handshaking Matching**

In one-way handshaking matching, it is possible that one vertex will have extended hands from multiple neighbors, but at most one of these neighbors can be matched. This may aﬀect the matching eﬃciency. For example, in Figure 2 (b), both vertex D and vertex H extend hands to vertex E. However, since vertex E’s strongest neighbor is vertex B, so neither vertex D nor H will be matched at this pass of handshaking in Figure 2 (b).

Instead of extending one hand, N-way handshaking matching allows each vertex to extend N hands (N > 1). We show an example of 2-way handshaking matching in Figure 3. In Figure 3(b), each vertex extends (up to) 2 hands at once, which extends to its strongest and second strongest neighbors. For example, vertex H extends one hand to vertex E which is its strongest neighbor and another hand to vertex I which is the second strongest neighbor.

In the next step, we take the edges whose two end points do not shake hands out of consideration (as if we “discard” edges). In Figure 3(b), there is no handshaking between vertex H and vertex E, so the edge HE is “as if” discarded (before next step in the same pass). After this, we will obtain an updated graph with a max degree N (N=2 in Figure 3(c)), we will refer to this graph as N-way graph in the remaining of the project description.

We now do one-way handshaking matching on the updated N-way graph. The matching on the N-way graph may yield more matched vertices. For example, in Figure 3(e), both vertex H and vertex D can be matched in the ﬁrst pass, while compared with one-way matching in Figure 2, both vertex H and vertex D have to be matched in the second pass.

### 1.3 POSIX threads – pthreads

A thread is deﬁned as an independent stream of instructions that can be scheduled to run in its own context. Multiple threads run in multiple active contexts.

Historically, hardware vendors have implemented their own proprietary versions of threads. For UNIX systems, a standardized C language threads programming interface has been speciﬁed by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or pthreads[1].

For more details on how to use pthreads API to write a parallel program and how to compile a pthreads program, please refer to the pthread tutorial [1] and the recitations.

## 2 Implementation

In this project, you will be asked to do the following:

1) implement the parallel one-way handshaking matching algorithm

2) Extra Credit: implement the parallel N-way handshaking matching algorithm

### 2.1 Data Structure

Un-directed weighted graph In this project, we use adjacency lists to represent an undirected weighted graph. There are four arrays: index[], oﬀset[], degree[], and weight[]. The array index[] keeps the neighbor lists of all vertices, for instance, node v 0 ’s neighbor list is followed by neighbor v 1 ’s neighbor list, and so on. The array oﬀset[] stores where a node’s neighbor list starts in the index[] array. The corresponding weight[] array stores the weight of each edge. The array degree[] stores the degree of each vertex, which is the number of neighbors for each vertex.

An example of adjacency array representation is shown in Figure 4. For vertex v 2 , its degree is 2 (degree[2] = 2). The oﬀset[2] value is 5 which means its neighbor list starts at index[5], thus index[5] = 5 (corresponding to vertex v 5 ), index[6] = 1 (corresponding to vertex v 1 ), and vertex v 5 and vertex v 1 are two neighbors of vertex v 2 .

Pleas be aware that within a neighbor list (of a speciﬁc vertex), the neighbors are sorted in descending order such that the strongest neighbor is always placed as the ﬁrst item and the second strongest neighbor is placed as the second item and so on. We already sorted the neighbors of each vertex for you. You do not have to sort them to ﬁnd the strongest neighbor, however, you do need to ﬁlter out the nodes that are already matched from previous passes.

We have provided graph I/O functions and code for reading/parsing the graphs, the pointers to the four arrays: index[], oﬀset[], weight[], and degree[] are stored in GraphData struct in the provided code package. Here is what GraphData looks like:

struct GraphData { int nNodes; int nEdges; int *oﬀset; int *degree; int *index; double *weight; }

In the main function of the provided code package, it calls readmm(inputFile, &graph) to read and parse an input graph ﬁle into the data object graph. Please check DataStructure.h and utils.c for more details.

N-way graph In N-way handshaking matching algorithm, you need to generate the N-way graph. We use two arrays to represent the N-way graph: nWayGraphDegree[] and nWayGraph[]. nWayGraphDegree[] is an array that stores the degree of each vertex in the N-way graph. nWayGraph[] represents the adjacency list of the nodes in the N-way graph. Since every node in the N-way graph has at most N neighbors, we allocate N*node_number elements for the nWayGraph[] array, such that the neighbors of the vertex v i are stored starting from nWayGraph[i * N] to at most nWayGraph[i * N + nWayGraphDegree[i]-1] in descending order such that the strongest neighbor are placed ﬁrst. In Figure 5, we show an example of the 2-way graph.

Matching results The matching algorithm runs for as many passes as it needs, until no edge can be added into the matching. It is possible that some vertices cannot be matched to any neighbor.

In this project, we use the array res[] to store the matching results. We provide several deﬁned constants to represent the status of a vertex in match.h. The array res[] is initialized to UNMATCHED (-1) for each vertex. When the graph matching program terminates, for vertex i, res[i] is either its matched vertex index or NO_MATCHED_NODES (-2) which represents vertex i is not matched. In general:

if vertex i and vertex j are matched initial value vertex i is not matched

In the main function, it calls write_match_result(outputFile, res, nNodes) to write the matching results stored in res into output ﬁle outputFile. nNodes represents the total number of vertices.

### 2.2 Work Balancing by Vertices

To implement parallel handshaking-based matching algorithms, we need to map the tasks into diﬀerent threads in a load balanced way. In this project, each thread will be in charge of a subset of vertices. A global barrier synchronization is performed after each pass of matching (the barrier code is already provided for you). The algorithm distributes the vertices evenly to the co-running threads. Assuming there are nNodes vertices to be processed, the total number of threads is threadNum, each thread will process around nNodes/threadNum

6 nodes. Since nNodes is not necessarily a multiply of threadNum, the last thread might be assigned ≤ nNodes/threadNum vertices. The code snippet in Figure 6 shows how each thread should ﬁnd the set of the vertices it is in charge of. Please use this vertex balancing method for all your parallel function implementation.

### 2.3 One-Way Handshaking

We have described the one-way handshaking matching algorithm in Section 1.2.1. In one-way handshaking, each vertex extends a hand to its strongest neighbor. Next it checks if there is a handshaking between itself and its strongest neighbor.

You only need to complete the following functions in oneway.c:

1. extend_one_hand(int threadId, int threadNum, GraphData graph, int nodeNum, int *nodeToProcess, int *res, int *strongNeighbor) Function Description: Each thread needs to be assigned a subset of vertices from nodeToProcess[] array. See Section 2.2 for load balancing method. For each vertex with ID v in this subset, ﬁnd its strongest neighbor and store the vertex index in the array strongNeighbor[v]. The pseudo code is shown below.

Input Parameters:

threadId: The thread index threadNum: Total thread number graph: The graph object nodeNum: The number of vertices nodeToProcess Each element is a vertex ID, and this array is usually used to pass the unmatched vertices to the processing function. The size of the array is nodeNum. res: The array that stores the matching status for each vertex. The size of the array is the total number of vertices in the graph.

Output Parameters:

strongNeighbor: The array that stores the index of the strongest neighbor for each node. The size of the array is the total number of vertices in the graph.

e.g. strongNeighbor[] = {3,4,5,4,1,2,3,4,7} for the example in Figure 2

2. check_handshaking(int threadId, int threadNum, int nodeNum, int *nodeToProcess, int *strongNeighbor, int *res)

Function Description:

Each thread needs to be assigned a subset of vertices from nodeToProcess[] array. See Section 2.2 for load balancing method. For each vertex v in this subset, given its strongest neighbor strongNeighbor[v], update res[v] correspondingly. The pseudo code is shown below.

V i = the set of vertices assigned to thread i

for each v in V i do

s = strongNeighbor[v]

if (v == strongNeighbor[s]) then res[v] = s

end if end for

Input Parameters:

threadId: Thread index threadNum: Total thread number nodeNum: The number of vertices nodeToProcess Each element is a vertex ID, and this array is usually used to pass the unmatched vertices to the processing function. The size of the array is nodeNum. strongNeighbor: The array that stores the index of the strongest neighbor for each vertex. The size of the array is the total number of vertices in the graph.

Output Parameters:

res The array that stores the matching status for each vertex. The size of the array is the total number of vertices in the graph.

E.g. res[] = {-1,4,5,-1,1,2,-1,-1,-1} for the example in Figure 2.

**程序代写代做C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB**

本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！

**E-mail:** [email protected] **微信:**dmxyzl003

如果您使用手机请先保存二维码，微信识别。如果用电脑，直接掏出手机果断扫描。