You are on page 1of 12

School of Sciences | Department of Computer Science and Engineering

CSC 608 - Graph Theory and Applications in Networks


Fall Semester 2016
Assignment 2

Fall Semester 2016


Assignment 2

Yiannis Hadjiargyrou
Savvas Savvides
Table of Contents
TABLE OF CONTENTS..............................................................................2

1 INTRODUCTION................................................................................3

2 GETTING STARTED..........................................................................4

3 PROVE..................................................................................................4

4 HOW TO SOLVE IT............................................................................5

5 TIME COMPLEXITY OF ALGORITHM........................................6

6 PSEUDOCODE AND EXPLANATION............................................6

7 TESTING.............................................................................................11

8 CONCLUSION:..................................................................................12

Enter your name here Page |2


1 Introduction
We must describe and implement an efficient program to determine, given
all the data described in the above paragraph and solve the question.

Suppose you are running a web site that is visited by the same set of people
every day. Each visitor claims membership in one or more demographic
groups; for example, a visitor might describe himself as male, 4050 years
old, a father, a resident of Paphos, an academic, a blogger, and a fan of
Michel Jackson. Your site is supported by advertisers. Each advertiser has
told you which demographic groups should see its advertisements and how
many of its advertisements you must show each day. Altogether, there are n
visitors, k demographic groups, and m advertisers.
Question: Describe an efficient algorithm to determine, given all the data
described in the previous paragraph, whether you can show each visitor
exactly one ad per day, so that every advertiser has its desired number of ads
displayed, and every ad is seen by someone. in an appropriate demographic
group.

Enter your name here Page |3


2 Getting Started
We form a network model of the given problem. Starting with a source
vertex s and a sink vertex t, we add vertices D1, . . ., Dn to represent the n
visitors, vertices P1, . . ., Pm to represent each of the m advertiser. . For
i=1, . . ., n, we construct an arc (s, Di). For j=1, . . ., m, we add the arc (Di,
Pj) if the visitor Di and the advertiser Pj has the same demographic groups.
(Note that since a visitor and an advertiser may have joint appointments, we
may have more than one incoming arc to any vertex Pj.) Moving on, for j=1,
., m, we construct an arc (Pj, t).
Assign capacity 1 to arcs from s to Di in the network, infinity capacity to
arcs Di to Pj and Pi to t capacity is related of how many of its ads advertiser
Pj want to show each day. (see Figure 1)

Figure 1Prove Graph

Introduction

3 Prove
We now prove that this network enforces the constraints of the original
problem
Each visitor is seen exactly one ad per day.
This is guaranteed by the arcs (s,Di) of capacity 1.

Enter your name here Page |4


Each visitor and advertiser is eligible to represent any
group to which he has an appointment and every ad is seen
by someone in an appropriate demographic group.
This is ensured by the fact that there exist arcs (Di, Pj) for
every visitor Di to which advertiser Pj is appointed.
Each visitor and advertiser chooses membership in one or
more demographic groups
This is guaranteed by the arcs (Di, Pi) of infinity capacity.

Every advertiser has its desired number of ads displayed.


Every arc (Pj, t) have capacity of how many each advertiser
wants to show its advertisements each day.

The last proof cannot be guaranteed, as the number of ads that will show
commensurate with the number of visitors we have.
Example
If you have 6 visitors, we will show 6 ads since every visitor is see one ad
per day. So, if an advertiser want to show 8 ads per day this is impossible.
We have shown the network correctly models the problem of Customized
Advertisements in a web site. Hence, we conclude that a valid committee
exists precisely when the max flow of the constructed network has value n.

4 How to solve it
We do some search and analysis about the problem and we decide that we
need to do match function, then Breadth-first search (BFS) and at the end
max flow to resolve it.
First, we must say that our demographic groups are stable to become the
matching right. (see Figure 2)
Now at the beginning we ask the visitors about the details we need from
them (age, sex, interests and job) and then advertisers put the target group of
their ads and how many times they want to show each ad. Both of visitors
and advertisers we save them in 2D array for each one.
After we take the necessary information needed we apply a match function
to find with what demographic group every ad match base on the targets
give the advertiser. We store this matching to a text file. Then we insert the

Enter your name here Page |5


text file to FordFulkerson algorithm (FFA) (max flow function) to find the
max flow of our matching. Inside max flow function also used Breadth-first
search (BFS) to find all the available paths (augmenting paths).

15-30 -> 30-50 ->


Age young adult
Sex Male Female
Interes
ts Sports Arts Music
Economi Scienc Figure
Job Student Business cs e 2Stable
demographic
Groups. At the first column, we see the demographic groups. At the row of each demographic group
we see the choices of each one

5 Time complexity of Algorithm


The runtime of FordFulkerson is bounded by O (E f), where E is the
number of edges in the graph and f is the maximum flow in the graph. This
is because each augmenting path can be found in O(E) time and increases
the flow by an integer amount of at least 1.

6 Pseudocode and explanation

elow we will analyse our code using pseudocode and an example with 5
visitor and 4 advertiser.

For every visitor base of the number of visitors we have we create a unique
id for each one. Then we ask them to tell as their age, sex, interests and job.
We save them in visitors array. The same procedure happens for the
advertisers.

Enter your name here Page |6


First, we ask how many visitors the web page have:
printf("How many visitors do you have per day: ");
scanf("%d", &n);
How to take the input:
for i<number of visitors
visitorID
read age
save in visitors[i][1]
read sex
save in visitors[i][2]
read interests
save in visitors[i][3]
read job
save in visitors[i][4]

Interest
Id Age Sex s Job
1 young male Sports student
femal bussines
2 young e Arts s

Enter your name here Page |7


economic
3 adult male Sports s
femal
4 adult e Music science
femal
5 young e Music student
Figure 3Example of visitor Table

ID Demands
6 male, sport
7 young, student
8 adult, music
9 female, arts
Figure 4Example of advirtiser Table

No.
Display
1
2
2
0
Figure 5Desired number of ads displayed of each advertiser

Then we do the matching between the two 2d array


for k less than advertiser do
for j less than visitors do
if advertiser target is equal to visitors information save the id of
both
if (adv[k][3]== visitors[j][3])
match[j][0] = visitor id
match[j][1] = advertiser id
save the result to text file
Also, we count the number of how many equalities we found, to
find at the end how many edges we must be process from the
maxflow algorithm.
count=count+1

Enter your name here Page |8


So, to find the the number of edges we count the number of
visitor plus the number of advertiser (since from source to each
visitor and from each advertiser to t we have ab edge), plus the
number of variable count.
edges= n+m+count;
Additionally, we calculate the number of nodes which is
nodes=2+n+m;
Is plus two because of the source and t node we add.

Figure 6 Example of text file

Here is the text file that we produce:


At the first line the first number is the total number of nodes and the second
the total number of edges.

Enter your name here Page |9


Then we have 26 lines with 3 numbers each line.
Is 26 because is the edges we have in our graph as seen before.
The first two number tell us where we have an edge(matching) and the third
is the capacity of each edge.
If you notice the last 3 lines, the third number of each line is the same as the
Figure 5.

Before max flow function run we read the values of the file. First
read the number of nodes and the number of edges.
fscanf(input,"%d %d",&no,&e);
Also we read each line separately from the file and store it as an 0
to 1 is equal to capacity 1 for example, until reach the number of
edges which means the end of the file.
for (i=source; i<number of edges(26); i++) {
fscanf(input,"%d %d %d",&a,&b,&c);
capacity [0][1] = 1;
}
Max flow algorithm with breadth-first search algorithm:
Let G (V, E) be a graph, and for each edge from u to v, let c (u, v)
be the capacity and f (u, v) be the flow. We want to find the
maximum flow from the source s to the sink t.
FordFulkerson
Inputs: Given a Network G= (V, E) with flow capacity c, a source
node s, and a sink node t
Output: Compute a flow f from s to t of maximum value
f (u, v) 0 for all edges (u, v)
While there is a path p from s to t in Gf, such that cf (u, v)>0 for
all edges (u, v) include in p (The path is founded by breadth-first
search algorithm which is explore the neighbor to determine an
augmenting path)
Find cf(p)=min {cf (u, v): (u, v)} include in p
For each edge (u, v) included in p
f (u, v) f (u, v) + c(p) (Send flow along the path)

Enter your name here P a g e | 10


f (v, u) f (v, u) - c(p) (The flow might be "returned" later)

When bfs cannot found more paths, s will not be able to t in the
residual network. If S is the set of nodes reachable by s in the
residual network, then the total capacity in the original network of
edges from S to the remainder of V is on the one hand equal to
the total flow we found from s to t and on the other hand serves
as an upper bound for all such flows. This proves that the flow we
found is maximal.

7 Testing
ID Demands
6 male, sport
7 Inputs:
young, student
8 adult, music
9 female, arts

Id Age Sex Interests Job Visitors:


1 Young male sports student
2 Young female Arts business
3 Adult male sports economics
4 Adult female music science
5 Young female music student

Advertisers:
No.
Display
2
2
1
0

Enter your name here P a g e | 11


Results:

Figure 7 Program Results

8 Conclusion:
In figure 7 we see the five feasible flow, also the max flow which is 5 hence,
we conclude that a valid committee exists precisely when the max flow of
the constructed network has value n(visitors).
Furthermore, we see that the number of adds which we display is satisfied
the number of how many of its ads each advertiser want to show each day.
Each visitor and advertiser is eligible to represent any group to which he has
an appointment and every ad is seen by someone in an appropriate
demographic group if you see in Figure 7 and if you compare it with the
inputs you will understand that we did.
And each visitor is displayed exactly one time in the results.

Enter your name here P a g e | 12

You might also like