{"id":8384,"date":"2018-04-02T00:02:12","date_gmt":"2018-04-02T00:02:12","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=8384"},"modified":"2023-01-09T12:55:33","modified_gmt":"2023-01-09T12:55:33","slug":"python-graph-network-example","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/python-graph-network-example\/","title":{"rendered":"Python Graph Network Example"},"content":{"rendered":"<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">Graph Network for a Set of People Using Python<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The idea is to build a network of people using graph methodology. In the graph, network nodes will represent people and edges will represent some kind of connection between them. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Every person has its name and attributes. Every relation has its name and attributes.<\/span><\/p>\n<p><!--more--><\/p>\n<p><span style=\"font-weight: 400;\">I represented the idea on the paper:<\/span><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8386 size-large\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1-1024x699.png\" alt=\"\" width=\"604\" height=\"412\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1-1024x699.png 1024w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1-300x205.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1-768x524.png 768w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/03\/python_graph_1.png 1210w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">It is an interesting task, because in fact all social networks use a similar model to connect people.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is the source code in Python with comments:<\/span><\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\nclass Network(object):<br \/>\n    def __init__(self):<br \/>\n        self.__network_dict = {}<br \/>\n        self.__person_property = {}<br \/>\n        self.__relation_property = {}<\/p>\n<p>    def create_person(self):<br \/>\n        print(&quot;enter person&quot;)<br \/>\n        person = input()<br \/>\n        if person not in self.__network_dict:<br \/>\n            self.__network_dict[person] = []<br \/>\n        #print(self.__network_dict)<\/p>\n<p>    def add_person_property(self, person, prop, value):<br \/>\n        if person in self.__network_dict:<br \/>\n            if person not in self.__person_property:<br \/>\n                self.__person_property[person] = {prop: value}<br \/>\n            else:<br \/>\n                self.__person_property[person].update({prop: value})<\/p>\n<p>    def add_relation(self, person1, person2):<br \/>\n        if person1 and person2 in self.__network_dict:<br \/>\n            self.__network_dict[person1].append(person2)<br \/>\n            self.__network_dict[person2].append(person1)<br \/>\n        else:<br \/>\n            self.__network_dict[person1] = [person2]<br \/>\n            self.__network_dict[person2] = [person1]<\/p>\n<p>    def add_relation_property(self, person1, person2, prop, value):<br \/>\n        prop_set = frozenset([person1, person2])<br \/>\n        if person1 in self.__network_dict and person2 in self.__network_dict[person1]:<br \/>\n            if prop_set not in self.__relation_property:<br \/>\n                self.__relation_property[prop_set] = {prop: value}<br \/>\n            else:<br \/>\n                self.__relation_property[prop_set].update({prop: value})<br \/>\n        #print(self.__relation_property)<\/p>\n<p>    def get_person(self, name):<br \/>\n        if name in self.__network_dict:<br \/>\n            return (self.__network_dict[name])<\/p>\n<p>    def friends_of_friends(self, name):<br \/>\n        # get a list of friends of friends of the person with given name<br \/>\n        friend_of_friends = []<br \/>\n        if name in self.__network_dict:<br \/>\n            for friend in self.__network_dict[name]:<br \/>\n                friend_of_friends.append(friend)<br \/>\n                for person in self.__network_dict[friend]:<br \/>\n                    friend_of_friends.append(person)<br \/>\n        #print(self.__network_dict)<br \/>\n        return list(set(friend_of_friends))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<br \/>\n    &quot;&quot;&quot;EXAMPLE&quot;&quot;&quot;<br \/>\n    network = Network()<br \/>\n    for i in range(2):<br \/>\n        network.create_person()<br \/>\n    for i in range(2):<br \/>\n        print(&quot;enter person1&quot;)<br \/>\n        person1 = input()<br \/>\n        print(&quot;enter person2&quot;)<br \/>\n        person2 = input()<br \/>\n        network.add_relation(person1,person2)<br \/>\n    print(&quot;enter person&#8217;s name&quot;)<br \/>\n    print(&quot;enter person name&quot;)<br \/>\n    person1 = input()<br \/>\n    person2 = input()<br \/>\n    print(&quot;prop&quot;)<br \/>\n    prop = input()<br \/>\n    print(&quot;value&quot;)<br \/>\n    value = input()<br \/>\n    network.add_relation_property(person1, person2, prop, value)<\/p>\n<p>    print(&quot;enter person&#8217;s name&quot;)<br \/>\n    print(&quot;enter person name&quot;)<br \/>\n    person1 = input()<br \/>\n    person2 = input()<br \/>\n    print(&quot;prop&quot;)<br \/>\n    prop = input()<br \/>\n    print(&quot;value&quot;)<br \/>\n    value = input()<br \/>\n    network.add_relation_property(person1, person2, prop, value)<br \/>\n[\/code]<\/p>\n<p><span style=\"font-weight: 400;\">That\u2019s how we can create a simplified model of a social network. We can easily create users and connections between them, and add properties to both users and relations. <\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Thanks for your attention!<\/span><\/p>\n<h2>Python Assignments Assistance from Top Experts<\/h2>\n<blockquote><p><i><span style=\"font-weight: 400;\">If you can\u2019t understand your assignment about <\/span><\/i><i>Python graph network, <\/i><i><span style=\"font-weight: 400;\">check our example provided with source code and comments. When studying information technology at university or college, you will face lots of tasks that seem unmanageable for a wide range of reasons. You may be more interested in another programming language, have a more important task on your freelance job, or just have no time to complete your assignments by the deadline. For these and other cases, you can use <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">assignment help<\/a> of AssignmentShark. <\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">Our company provides expert help in fields of web programming, QA and software testing, data analysis, computer science, and IT assignments. If you have assignments which are of different subjects, such as biology or chemistry, you can also pass these tasks to our experts, for example, by requesting &#8220;<a href=\"https:\/\/assignmentshark.com\/do-my-accounting-homework.html\" target=\"_blank\" rel=\"noopener\">do my accounting homework for me<\/a>.&#8221; We have gathered a great team of professionals who have degrees in various fields of science and are skilled in academic writing.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">With our service you can extend your timetable for more important things! Include your requirements in the order form, and by the due date you will be holding a complete assignment created according to your needs.<\/span><\/i><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Graph Network for a Set of People Using Python The idea is to build a network of people using graph methodology. In the graph, network nodes will represent people and edges will represent some kind of connection between them. Every person has its name and attributes. Every relation has its name and attributes.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-8384","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=8384"}],"version-history":[{"count":8,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8384\/revisions"}],"predecessor-version":[{"id":13451,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8384\/revisions\/13451"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=8384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=8384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=8384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}