Python代写-PURMHFW 2 Q3
时间:2021-03-31
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 1/6
GO TO P2 Home | Q1 | Q2 | Q3
Before you start, run the next code fragment to download CSV (comma separated values) ¦les that
you will need to your colab environment ¦rst:
Q3
fURm¦XUllib.UeTXeVW¦imSRUW¦XUlUeWUieYe
imSRUW¦RV
¦
def¦dRZnlRad(XUl,¦file):
¦¦if¦RV.SaWh.iVfile(file):
¦¦¦¦SUinW(file¦+¦"¦alUead\¦dRZnlRaded.¦YRX¦can¦Vee¦iW¦if¦\RX¦click¦Rn¦Whe¦fRldeU¦icRn¦
¦¦elVe:
¦¦¦¦SUinW("DRZnlRading¦file¦"¦+¦file¦+¦"¦...",¦end="")
¦¦¦¦XUlUeWUieYe(XUl,file)
¦¦¦¦SUinW("OK")
¦
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/GUaShLab.S\','GUaShLab.S\')¦¦#¦GUaShLab)
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe1.cVY','caVe1.cVY')¦¦#¦caVe1.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe1a.cVY','caVe1a.cVY')¦#¦caVe1a.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe1b.cVY','caVe1b.cVY')¦#¦caVe1b.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe2.cVY','caVe2.cVY')¦¦#¦caVe2.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe3.cVY','caVe3.cVY')¦¦#¦caVe3.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe4.cVY','caVe4.cVY')¦¦#¦caVe4.cVY
dRZnlRad('hWWS://Ued.VmX.edX.Vg/UeVRXUceV/caVe5.cVY','caVe5.cVY')¦¦#¦caVe5.cVY
A clique is de¦ned as a group of nodes in a graph in which every node is connected to every other
node in the group directly. A k-clique is a clique of k nodes (e.g. a 4-clique is a clique with 4 nodes).
For this question, as long as user X follows user Y, you can say that X and Y have a relationship
(regardless of whether Y follows X in return). X and Y are connected, as long as one of them follows
the other.
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 2/6
Figure 2. A graph showing a simple Tweeter network with 11 users ( case1a.csY )
Figure 2 above is the visual representation of case1a.csY .
2-cliques are not so interesting because there are so many 2-cliques in this graph. As long as 2
nodes are connected by an edge, it forms a 2-clique.
There are many 3-cliques in this graph. For example:
[1, 2, 3] <-- a 3-clique
[1, 3, 6] <-- another 3-clique
[3, 5, 6]
[5, 6, 9]
[0, 5, 9]
[0, 8, 9]
[0, 8, 10]
[0, 9, 10]
[8, 9, 10]
There is only one 4-clique in this graph:
[0, 8, 9, 10] <-- This is a 4-clique because 0 has a relationship with 8, 9 and 10. 8 has a
relationship with 0, 9 and 10. 9 has a relationship with 0, 8 and 10. And 10 has a relationship
with 0, 8 and 9.
Write a function called get_clique(folloZers) that takes in a 2D list of followers that represents
a Tweeter social network. It then returns an identi¦ed clique as a list of IDs. You should aim to
maximize k , the clique size (i.e. if you return a 5-clique, your score will be 5, if you return a 8-clique,
your score will be 8. You want as high a score as possible). The order of the IDs in your returned
clique does not matter, as long as they form a clique.
Your Task
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 3/6
You can assume that the Tweeter users always have consecutive integer IDs starting from 0 , and
that s is always a valid user ID.
e.g.¦1:¦(caVe1a.cVY)
fROORZeUV_1a¦=¦[[5,¦8,¦10],¦[2,¦6],¦[1,¦3],¦[1,¦5],¦[2,¦5],¦[4,¦6,¦9],¦[1,¦3,¦9],¦[6],¦[7,¦10
geW_cOLTXe(fROORZeUV_1a)¦Pa\¦UeWXUQ¦aQ\¦Rf¦WKe¦fROORZLQg¦(VcRUeV¦LQdLcaWed):
¦¦[]¦¦¦¦¦¦¦¦¦¦¦¦¦#¦VcRUe¦=¦0¦<--¦TKLV¦LV¦VLOO\¦bXW¦cRUUecW
¦¦[9]¦¦¦¦¦¦¦¦¦¦¦¦#¦VcRUe¦=¦1¦<--¦TKLV¦LV¦VLOO\¦bXW¦cRUUecW
¦¦[2,¦1]¦¦¦¦¦¦¦¦¦#¦VcRUe¦=¦2
¦¦[7,¦8]¦¦¦¦¦¦¦¦¦#¦VcRUe¦=¦2
¦¦[6,¦5,¦3]¦¦¦¦¦¦#¦VcRUe¦=¦3
¦¦[1,¦3,¦2]¦¦¦¦¦¦#¦VcRUe¦=¦3
¦¦[8,¦9,¦0,¦10]¦¦#¦VcRUe¦=¦4
TKeUe¦cRXOd¦be¦PaQ\¦PRUe¦cRUUecW¦UeWXUQ¦YaOXeV.¦WKaW¦LV¦VKRZQ¦abRYe¦LV¦MXVW¦aQ¦e[aPSOe.¦
Fill up this function:
#¦EXTRACT¦THIS¦CODE¦&¦SUBMIT¦AS¦S2T3.S\¦TO¦RED!!!
#¦Filename:¦S2T3.S\
#¦Team¦ID:¦
¦
#¦E[ceSW¦imSRUW¦VWaWemenWV,¦all¦RWheU¦VWaWemenWV¦VhRXld¦Rnl\¦be¦in¦fXncWiRnV.
#¦imSRUW¦GUaShLab¦¦#¦<--¦XncRmmenW¦if¦\RX¦ZanW¦WR¦XVe¦GUaShLab
¦
def¦geW_cliTXe(fRllRZeUV):
¦¦#¦TODO:¦ediW¦WhiV¦fXncWiRn.
¦¦UeWXUn¦[0,¦1,¦2,¦3]
Run the following code ¦rst. These functions will be called by the test cases below.
Test Cases
imSRUW¦cRS\,¦Wime
#¦---------------------------------------------------------------------------
#¦UeadV¦a¦CSV¦file¦and¦UeWXUnV¦a¦2D¦liVW¦Rf¦inWV
#¦e.g.¦Uead_file("caVe1.cVY")¦Zill¦UeWXUn:¦[[2],¦[0,¦3],¦[0,¦1],¦[1,¦2,¦4,¦5],¦[1,¦6,¦
def¦Uead_file(file_name):
¦¦inSXW¦=¦[]
¦¦ZiWh¦RSen(file_name,¦"U")¦aV¦file:
¦¦¦¦fRU¦line¦in¦file:
¦¦¦¦¦¦line¦=¦line.UVWUiS("\n")
¦¦¦¦¦¦cXUUenW_liVW¦=¦line.VSliW(",")
¦¦¦¦¦¦inde[¦=¦inW(cXUUenW_liVW.SRS(0))¦¦#¦1VW¦elemenW¦iV¦Whe¦inde[.¦aVVXmSWiRn:¦Whe¦in
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 4/6
¦¦¦¦¦¦cXUUenW_liVW¦=¦[inW(i)¦fRU¦i¦in¦cXUUenW_liVW]¦¦#¦cRnYeUW¦all¦elemenWV¦fURm¦VWUin
¦¦¦¦¦¦inSXW.aSSend(cXUUenW_liVW)¦¦¦¦¦¦¦¦#¦inVeUW¦inWR¦liVW¦¦¦
¦¦UeWXUn¦inSXW
#¦---------------------------------------------------------------------------
#¦WakeV¦in¦an¦anVZeU¦(e.g.¦[1,2,7,1]).¦And¦UeWXUnV¦eiWheU
#¦-¦an¦eUURU¦meVVage¦(VWUing),¦RU
#¦-¦NRne¦(meaning¦WheUe¦iV¦nR¦eUURU¦ZiWh¦V\nWa[¦Rf¦anVZeU).¦¦
def¦geW_V\nWa[_eUU_mVg_T3(anVZeU,¦laUgeVW_id):
¦¦if¦anVZeU¦==¦NRne:
¦¦¦¦UeWXUn¦"EUURU:¦\RXU¦fXncWiRn¦UeWXUned¦NRne.¦IW¦VhRXld¦UeWXUn¦a¦liVW¦Rf¦inWegeUV."
¦¦elif¦W\Se(anVZeU)¦iV¦nRW¦liVW:
¦¦¦¦UeWXUn¦"EUURU:¦\RXU¦fXncWiRn¦UeWXUned¦VRmeWhing¦RWheU¦Whan¦a¦liVW.¦IW¦VhRXld¦UeWXU
¦¦elif¦nRW¦all(iVinVWance(i,¦inW)¦fRU¦i¦in¦anVZeU):¦¦#¦check¦if¦all¦elemenWV¦in¦anVZeU
¦¦¦¦UeWXUn¦"EUURU:¦\RXU¦fXncWiRn¦UeWXUned¦a¦liVW¦Rf¦elemenWV,¦bXW¦nRW¦all¦Rf¦Whem¦aUe¦
¦¦elif¦nRW¦all(i<=laUgeVW_id¦and¦i>=0¦fRU¦i¦in¦anVZeU):
¦¦¦¦UeWXUn¦"EUURU:¦WheUe¦aUe¦elemenWV¦in¦\RXU¦anVZeU¦WhaW¦aUe¦negaWiYe¦RU¦e[ceed¦Whe¦l
¦¦elVe:
¦¦¦¦UeWXUn¦NRne¦¦#¦nR¦V\nWa[¦SURblem,¦nR¦eUURU¦mVg
#¦---------------------------------------------------------------------------
#¦id1¦and¦id2¦haV¦a¦UelaWiRnVhiS¦if¦eiWheU¦id1¦fRllRZV¦id2¦RU¦Yice¦YeUVa
def¦haV_UelaWiRnVhiS(id1,¦id2,¦fRllRZeUV):
¦¦UeWXUn¦(id1¦in¦fRllRZeUV[id2])¦RU¦(id2¦in¦fRllRZeUV[id1])
#¦---------------------------------------------------------------------------
#¦check¦if¦anVZeU¦iV¦a¦cliTXe,¦and¦calcXlaWe¦VcRUe.¦UeWXUnV¦eiWheU¦NRne¦(nRW¦a¦cliTXe)
def¦geW_VcRUe_T3(anVZeU,¦fRllRZeUV):
¦¦if¦anVZeU¦==¦[]:
¦¦¦¦UeWXUn¦0¦#¦VcRUe¦iV¦0
¦¦if¦len(anVZeU)¦==1:
¦¦¦¦UeWXUn¦1¦#¦VcRUe¦iV¦1¦¦
¦
¦¦#¦check¦WhaW¦all¦elemenWV¦in¦anVZeU¦fRUm¦a¦cliTXe
¦¦fRU¦i¦in¦Uange(0,¦len(anVZeU)):
¦¦¦¦cXUUenW_id¦=¦anVZeU[i]
¦¦¦¦fRU¦j¦in¦Uange(i¦+¦1,¦len(anVZeU)):
¦¦¦¦¦¦RWheU_id¦=¦anVZeU[j]
¦¦¦¦¦¦if¦nRW¦haV_UelaWiRnVhiS(cXUUenW_id,¦RWheU_id,¦fRllRZeUV):
¦¦¦¦¦¦¦¦SUinW("EUURU:¦WheUe¦iV¦nR¦UelaWiRnVhiS¦beWZeen¦WheVe¦WZR¦XVeUV¦:"¦+¦VWU(cXUUen
¦¦¦¦¦¦¦¦UeWXUn¦NRne¦#¦anVZeU¦iV¦ZURng
¦
¦¦#¦UeWXUn¦VcRUe
¦¦UeWXUn¦len(anVZeU)¦#¦VcRUe
#¦---------------------------------------------------------------------------
#¦WeVW¦caVe
def¦UXn_WeVW_caVe_T3(WeVW_caVe_name,¦file_name):
¦¦SUinW("TeVW¦CaVe¦:"¦+¦WeVW_caVe_name¦+¦",¦file_name¦:"¦+¦file_name)
¦
¦¦fRllRZeUV¦=¦Uead_file(file_name)
¦¦fRllRZeUV_clRne¦=¦cRS\.deeScRS\(fRllRZeUV)¦#¦make¦a¦clRne.¦WhiV¦clRne¦Zill¦be¦SaVVed
¦¦VWaUW_Wime¦=¦Wime.Wime()
¦¦anVZeU¦=¦geW_cliTXe(fRllRZeUV_clRne)¦¦#¦callV¦\RXU¦fXncWiRn
¦¦Wime_Waken¦=¦Wime.Wime()¦-¦VWaUW_Wime
¦¦SUinW("E[ecXWiRn¦Wime¦"¦+¦VWU(Wime Waken)¦+¦"¦VecRndV.\n")¦¦¦¦#¦diVSla\¦Wime¦laSVed
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 5/6
S ( ( _ ) ) S \ S
¦
¦¦#¦SUinW¦RXWcRme
¦¦SUinW("YRXU¦fXncWiRn¦UeWXUned:¦"¦+¦VWU(anVZeU))
¦
¦¦#¦check¦V\nWa[¦cRUUecWneVV
¦¦laUgeVW_id¦=¦len(fRllRZeUV)¦-¦1
¦¦eUU_mVg¦=¦geW_V\nWa[_eUU_mVg_T3(anVZeU,¦laUgeVW_id)
¦¦if¦eUU_mVg¦!=¦NRne:
¦¦¦¦SUinW(eUU_mVg)
¦¦¦¦SUinW("TeVW¦caVe¦"¦+¦WeVW_caVe_name¦+¦"¦*****¦FAILED¦*****")
¦¦¦¦UeWXUn¦NRne
¦¦
¦¦#¦geW¦VcRUe
¦¦VcRUe¦=¦geW_VcRUe_T3(anVZeU,¦fRllRZeUV)
¦¦if¦VcRUe¦==¦NRne:
¦¦¦¦SUinW("TeVW¦caVe¦"¦+¦WeVW_caVe_name¦+¦"¦*****¦FAILED¦*****")
¦¦elVe:
¦¦¦¦SUinW("TeVW¦caVe¦"¦+¦WeVW_caVe_name¦+¦"¦SaVVed")
¦¦¦¦SUinW("ScRUe¦:¦"¦+¦VWU(VcRUe))¦¦¦¦
These are test cases to check your get_clique function. You are encouraged to create your own
test cases to test your function using the other CSV ¦les. Remember that your algorithm is expected
to work with all possible Tweeter networks.
#¦WeVW¦caVe¦T3_1a¦(caVe1a.cVY)
file_name¦=¦"caVe1a.cVY"
UXn_WeVW_caVe_T3("T3_caVe1a",¦file_name)
#¦WeVW¦caVe¦T3_2¦¦(caVe2.cVY)
file_name¦=¦"caVe2.cVY"
UXn_WeVW_caVe_T3("T3_caVe2",¦file_name)
#¦WeVW¦caVe¦T3_3¦¦(caVe3.cVY)
file_name¦=¦"caVe3.cVY"
UXn_WeVW_caVe_T3("T3_caVe3",¦file_name)
#¦WeVW¦caVe¦T3_4¦¦(caVe4.cVY)
file_name¦=¦"caVe4.cVY"
UXn_WeVW_caVe_T3("T3_caVe4",¦file_name)
#¦WeVW¦caVe¦T3_5¦¦(caVe5.cVY)
file_name¦=¦"caVe5.cVY"
UXn_WeVW_caVe_T3("T3_caVe5",¦file_name)
31/03/2021 CRS\ RI PURMHFW 2 Q3 - CROaERUaWRU\
KWWSV://FROaE.UHVHaUFK.JRRJOH.FRP/GULYH/1MXZaWaOAF6LBHYYTTWRPU7YW\T8DM[IR#SULQWMRGH=WUXH 6/6
p2q3.p\ to red
report (max 3 slides for all questions) to elearn
To Submit:
1 mark for correctness.
2 marks for speed.
5 marks for quality.
Other test cases will be used on red . You must ensure that your function does not time-out on
red . A correct answer that does not time out on red will get at least 1 mark. You will only get
marks for speed and quality if your solution is correct.
~END
Assessment:
GO TO P2 Home | Q1 | Q2 | Q3