Vous êtes sur la page 1sur 12

MD laboratory work 2

Pascari Pavel may 2013

The only Problem

You have a set of 20 people connected via a friendship matrix. The whole list is given in 1 1.txt.

1.1
Problem: Find the person with the most friends. SOLUTION: f = open ( 1 1 . t x t , r ) #read t h e a d j a n c y t a b l e from f i l e l = [] l = [ l i n e . s p l i t ( ) f o r l i n e in f ] print ( \ tThe a d j a c e n c y matrix : ) for l i n e in l : print l i n e #p r i n t t h e graph friend = [ ] #i n i n t i a l i z e a l i s t w i t h IDs o f f r i e n d s o f each guy for row in l : count = 0 for i in row : i f i == 1 : count+=1 f r i e n d . append ( count ) print f r i e n d maxim = max( f r i e n d ) #d e t e r m i n e t h e p e r s o n w i t h maximal number o f f r i e n d s print ( \ n \ nPerson + s t r ( f r i e n d . i n d e x ( maxim))+ have t h e maximum number o f f r i e n d s , e q u a l t o : +s t r ( maxim ) ) #i f t h e r e a r e more p e o p l e s w i t h t h e same number o f f r i e n d s for i in r a n g e ( l e n ( f r i e n d ) ) : i f i == f r i e n d . i n d e x ( maxim ) : continue i f f r i e n d [ i ] == maxim : print ( \ nAlso such a number o f f r i e n d s have p e r s o n with i n d e x : )+ s t r ( i )

1.2
Problem: Sort all people by the number of friends. SOLUTION: The easiest way is to create a dictionary with the ID of a person and respectively the number of friends that this person have. f r i e n d d i c t = {} for i in r a n g e ( l e n ( f r i e n d ) ) : f r i e n d d i c t . update ( { i : f r i e n d [ i ] } ) #s o r t t h e d i c t i o n a r y print ( The p e r s o n s i n d e x > n r o f f r i e n d s ( on t h e i n c r e a s e ) ) for key , v a l u e in s o r t e d ( f r i e n d d i c t . i t e r i t e m s ( ) , key=lambda ( k , v ) : ( v , k ) ) : print %s > %s % ( key , v a l u e )

1.3

Lets create our own Linked In. Some Black Jack would be nice.

Lets say you rate people in the following way: * 64 points for the rst-level connection; * 32 points for second-level connection; * 16 points for the third-level and so on; How to do that? Well, each person in the graph is connected to everyone else at some level. Therefore, each person will have a list of connections which is as long as the total list of people in the graph(20 in our case). You then have to compute the shortestpath from each of the nodes to each of the other nodes. For example, lets say that you found that from node 0 you can reach to node 3 in 5 steps(that is, the shortest path connecting nodes 0 and 3 has 5 steps). That means that node 3 will be a connection 5 to node 0 and will therefore contribute to 0 with 4 points. As a procedure, you can take each item n and then compute the distances between n and all the other vertices of the graph. You can use these distances to compute the value that is added by each of the other n 1 vertices to n. sum it and youll have the value of vertex n. In order to nd the shortest path between two vertices, youll have to use the Dijkstra salgorithm. You can nd a plenty of implementations of that algorithm online. Problem: Compute the points for each person in our Network. Lets call it Rating.

SOLUTION: from c o l l e c t i o n s import namedtuple from p p r i n t import p p r i n t a s pp

inf = float ( inf ) Edge = namedtuple ( Edge , s t a r t , end , c o s t ) c l a s s Graph ( ) : def i n i t ( s e l f , edges ) : s e l f . e d g e s = e d g e s 2 = [ Edge ( edge ) f o r edge in e d g e s ] s e l f . v e r t i c e s = s e t ( sum ( ( [ e . s t a r t , e . end ] f o r e in e d g e s 2 ) , [ ] ) ) def d i j k s t r a ( s e l f , s o u r c e , d e s t ) : a s s e r t s o u r c e in s e l f . v e r t i c e s d i s t = { v e r t e x : i n f f o r v e r t e x in s e l f . v e r t i c e s } p r e v i o u s = { v e r t e x : None f o r v e r t e x in s e l f . v e r t i c e s } dist [ source ] = 0 q = s e l f . v e r t i c e s . copy ( ) n e i g h b o u r s = { v e r t e x : s e t ( ) f o r v e r t e x in s e l f . v e r t i c e s } f o r s t a r t , end , c o s t in s e l f . e d g e s : n e i g h b o u r s [ s t a r t ] . add ( ( end , c o s t ) ) #pp ( n e i g h b o u r s ) while q : u = min ( q , key=lambda v e r t e x : d i s t [ v e r t e x ] ) q . remove ( u ) i f d i s t [ u ] == i n f or u == d e s t : break f o r v , c o s t in n e i g h b o u r s [ u ] : alt = dist [ u ] + cost if alt < dist [v ] : dist [v] = alt previous [ v ] = u #pp ( p r e v i o u s ) s , u = [ ] , dest while p r e v i o u s [ u ] : s . insert (0 , u) u = previous [ u ] s . insert (0 , u) return s

r a t i n g =0 mylist =[] for m in r a n g e ( 2 0 ) : r a t i n g 1 =0 fo r n in r a n g e ( 2 0 ) : l e n g t h=l e n ( graph . d i j k s t r a ( { : d } . format (m) , { : d } . format ( n ) ) ) i f l e n g t h == ( 1 or 2 ) : r a t i n g =64 e l i f l e n g t h == 3 : r a t i n g =32 e l i f l e n g t h == 4 : r a t i n g =16 e l i f l e n g t h == 5 : r a t i n g =8 e l i f l e n g t h == 6 : r a t i n g =4 e l i f l e n g t h == 7 : r a t i n g =2 e l i f l e n g t h == 8 : r a t i n g =1 e l i f length > 8: r a t i n g =0 r a t i n g 1=r a t i n g 1+r a t i n g l e n g t h=0 print ( \ n r a t i n g f o r { : d } p e r s o n : . format (m)+ s t r ( r a t i n g 1 ) ) m y l i s t . append ( r a t i n g 1 )

1.4

Take photos of your food more often

Lets say that each of these people has a certain rate of posting content. Obviously, people whole communicate more are much more inuential. Suppose that you need to promote a new brand using the social media. We found out how often each of these 20 people writes something on their walls. You can nd it in 1 5.txt. Whom of these people will you contact? why? be advised that not only the frequency of posting matters, but also the number of friends! Use the data from the previous exercise and nd the new Rating for each person by multiplying it with 0.5 of the posting rate. Problem: Please list the people you will contact.

SOLUTION: f r e q u e n c y l i s t =[6 , 18 , 17 , 6 , 5 , 7 , 7 , 17 , 2 , 16 , 14 , 12 , 7 , 1 0 , 1 9 ,9 ,3 ,12 ,6 ,6] myFinalList = [ ] for i in r a n g e ( l e n ( m y l i s t ) ) : rating = f r e q u e n c y l i s t [ i ] mylist [ i ] 0.5 m y F i n a l L i s t . append ( i n t ( r a t i n g ) ) #a s o r t e d l i s t w i t h t h e d a t a from m y F i n a l L i s t sortedList = [ ] for i in r a n g e ( l e n ( m y F i n a l L i s t ) ) : s o r t e d L i s t . append ( m y F i n a l L i s t [ i ] )

sortedList . sort () sortedList . reverse () print ( \ nThe l i s t with t h e f i n a l r a t i n g o f each p e r s o n : ) print ( Rating \ t P e r s o n s number ) for i in r a n g e ( l e n ( s o r t e d L i s t ) ) : print ( s t r ( s o r t e d L i s t [ i ] ) + \ t + s t r ( myFinalList . index ( s o r t e d L i s t [ i ] ) ) )

1.5

Analyse you content

You are publishing a book and would like to promote it through the use of social media. the books title is From T-Rex to Justin Biber: How Internet has changed the Politics, Art, and cute Cats. You have done some research in the worlds most popular social network and have found that the range of interests is stored in 1 6.txt. Problem: Analyze your title and see what spectre of interests is your book marketable to. SOLUTION: g e n e r a l l i s t =[ videogames t r e x , b i e b e r , t h e a t r e books , music , movies , p e r s o n 0 =[ music , b i e b e r , cats , p o l i t i c s , , computers , a r t , i n t e r n e t , programming ] , i n t e r n e t , movies ]

p e r s o n 1 =[ programming , t r e x , computers , a r t ] p e r s o n 2 =[ computers , books , f o o t b a l l ] p e r s o n 3 =[ f o o t b a l l , c a t s ] p e r s o n 4 =[ i n t e r n e t , videogames ] p e r s o n 5 =[ programming , movies , c a t s ] p e r s o n 6 =[ p l a n e s , i n t e r n e t ] p e r s o n 7 =[ c a r s , i n t e r n e t ] p e r s o n 8 =[ movies , p l a n e s ] p e r s o n 9 =[ t h e a t r e , computers ] p e r s o n 1 0 =[ i n t e r n e t , t h e a t r e , books ] p e r s o n 1 1 =[ books , c a t s , programming ] p e r s o n 1 2 =[ videogames , b i e b e r , f o o t b a l l ] p e r s o n 1 3 =[ computers , f a r m i n g ] p e r s o n 1 4 =[ t h e a t r e , c a r s , videogames , p o l i t i c s ] p e r s o n 1 5 =[ f a r m i n g , i n t e r n e t ] p e r s o n 1 6 =[ b e e r , t h e a t r e ] p e r s o n 1 7 =[ f a r m i n g , c a t s , books , i n t e r n e t ] p e r s o n 1 8 =[ programming , t h e a t r e ] p e r s o n 1 9 =[ f a r m i n g , computers ] #Find t h e number o f a s s o c i a t i o n by comparing #t h e g e n e r a l l i s t and each p e r s o n l i s t p r s 0 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 0 ] ) ) p r s 1 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 ] ) ) p r s 2 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 2 ] ) ) p r s 3 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 3 ] ) ) p r s 4 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 4 ] ) ) p r s 5 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 5 ] ) ) p r s 6 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 6 ] ) ) p r s 7 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 7 ] ) ) p r s 8 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 8 ] ) ) p r s 9 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 9 ] ) ) p r s 1 0 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 0 ] ) ) p r s 1 1 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 1 ] ) ) p r s 1 2 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 2 ] ) ) p r s 1 3 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 3 ] ) ) p r s 1 4 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 4 ] ) ) p r s 1 5 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 5 ] ) ) p r s 1 6 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 6 ] ) ) p r s 1 7 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 7 ] ) ) p r s 1 8 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 8 ] ) ) p r s 1 9 =( l e n ( [ i f o r i in g e n e r a l l i s t i f i in p e r s o n 1 9 ] ) ) #c r e a t e a l i s t w i t h number o f

#each p e r s o n s a s s o c i a t i o n l i s t n r i n t s =[ prs0 , prs1 , prs2 , prs3 , prs4 , prs5 , prs6 , prs7 , prs8 , prs9 , prs10 , prs11 , prs12 , prs13 , prs14 , prs15 , prs16 , prs17 , prs18 , p r s 1 9 ] print ( \ n n u m b e r o f i n t e r e s t s f o r e a c h p e r s o n ) print ( l i s t n r i n t s )

1.6

Promote it!

We have provided you with a list of interests of each of these people. you can nd it in 1 7.txt. Considering the set of interests you have chosen, who of them would you market the book to? Lets say that a person has 5 of her interests coinciding with your books and she has Rating of 346. Multiply her rating with the 0.2* coinciding interests to see a nal score. Sort the people by this nal score. Problem: Provide us with a list of 5 people we should contact to make your book a best-seller! SOLUTION: print ( \ n R e s u l t s : ) f i n a l R e s u l t =[] # i n i t i a l i z e a l i s t f o r 5 p e r s o n s t o market w i t h listContact = [ ] sortedContact = [ ] #t h e l i s t w i t h t h e r a t i n g o f each p e r s o n : m y F i n a l L i s t for i in r a n g e ( l e n ( m y F i n a l L i s t ) ) : l i s t C o n t a c t . append ( m y F i n a l L i s t [ i ] l i s t n r i n t s [ i ] 0 . 2 ) for i in r a n g e ( l e n ( l i s t C o n t a c t ) ) : s o r t e d C o n t a c t . append ( l i s t C o n t a c t [ i ] ) sortedContact . sort () sortedContact . reverse () print ( \ nThe l i s t with t h e f i n a l r a t i n g o f each p e r s o n f o r promoting : ) print ( Rating \ t P e r s o n s number ) for i in r a n g e ( l e n ( s o r t e d C o n t a c t ) ) : print ( s t r ( s o r t e d C o n t a c t [ i ] ) + \ t + s t r ( l i s t C o n t a c t . index ( sortedContact [ i ] ) ) ) print ( 5 P e r s o n s t h a t you s h o u l d c o n t a c t a r e : ) for i in r a n g e ( 5 ) : print ( Rating +s t r ( s o r t e d C o n t a c t [ i ] ) + \ t P e r s o n number> + s t r ( l i s t C o n t a c t . i n d e x ( s o r t e d C o n t a c t [ i ] ) ) )

10

11

CONCLUSION: I think that this Laboratory Work was a challenge for me. This Laboratory Work helped me to understand better how to nd the shortest path in a graph, I think that it is important to emphasize that giving a real problem is the better way to understand the theory. An other thing that was helpful is that I learned how to import les in Python and how to manipulate better dictionaries and lists.

12

Vous aimerez peut-être aussi