Giter VIP home page Giter VIP logo

dsc-3-28-07-graph-theory-simple-shortest-path-lab-online-ds-pt-100118's Introduction

Graph Theory: Simple and Shortest Paths - Lab

Introduction

In this lab, we shall work with Florentine families graph. Click here to see details on Florantine family dataset. This dataset comes bundled with networkx as shown here. Visit these links to get some idea about the contents of this dataset. We shall use it as an undirected dataset for this lab and work on identifying paths between different nodes.

Objectives

You will be able to:

  • Load and study Florentine families graph generator in networkx
  • Calculate and visualize the different paths between a given set of nodes

Load and Draw Florentine families graph generator with a Fruchterman Reingold layout

Note: Refer to the documentation for methods to load the required graph and layout.

# Your code here 

png

Calculate shortest path between Medici and Peruzzi families

# Your code here 
['Medici', 'Barbadori', 'Castellani', 'Peruzzi']

You will see above that the shortest paths may not necessarily be unique i.e. there can be more than one , while counting the number of hops. We can use all_shortest_paths() method to find if there are more than one.

Calculate all shortest paths between Medici and Peruzzi families

# Your code here 
[['Medici', 'Barbadori', 'Castellani', 'Peruzzi'],
 ['Medici', 'Ridolfi', 'Strozzi', 'Peruzzi']]

Calculate and draw the shortest path between Lamberteschi and Ridolfi families

  • Create a function plot_paths(G, paths) that would take in a graph with calculated path(s)
  • Use fruchterman_reingold_layout()
  • For all paths in paths, color the edges and display the graph
  • Print the path in the output
source = 'Lamberteschi'
target = 'Ridolfi'
def plot_paths(G, paths):
    
    # Your code here 
    
    pass
    
print(nx.shortest_path(G, source, target))

plot_paths(G, [nx.shortest_path(G, source, target)])
['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Ridolfi']

png

Calculate and draw all simple paths between Lamberteschi and Ridolfi families

  • Use nx.all_simple_paths(G, source, target) to calculate all possible paths between two families and plot them.
# Your code here 
1 ['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Medici', 'Barbadori', 'Castellani', 'Peruzzi', 'Strozzi', 'Ridolfi']
2 ['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Medici', 'Barbadori', 'Castellani', 'Peruzzi', 'Bischeri', 'Strozzi', 'Ridolfi']
3 ['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Medici', 'Barbadori', 'Castellani', 'Strozzi', 'Ridolfi']
4 ['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Medici', 'Ridolfi']
5 ['Lamberteschi', 'Guadagni', 'Tornabuoni', 'Ridolfi']
6 ['Lamberteschi', 'Guadagni', 'Albizzi', 'Medici', 'Barbadori', 'Castellani', 'Peruzzi', 'Strozzi', 'Ridolfi']
7 ['Lamberteschi', 'Guadagni', 'Albizzi', 'Medici', 'Barbadori', 'Castellani', 'Peruzzi', 'Bischeri', 'Strozzi', 'Ridolfi']
8 ['Lamberteschi', 'Guadagni', 'Albizzi', 'Medici', 'Barbadori', 'Castellani', 'Strozzi', 'Ridolfi']
9 ['Lamberteschi', 'Guadagni', 'Albizzi', 'Medici', 'Ridolfi']
10 ['Lamberteschi', 'Guadagni', 'Albizzi', 'Medici', 'Tornabuoni', 'Ridolfi']
11 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Castellani', 'Strozzi', 'Ridolfi']
12 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Castellani', 'Barbadori', 'Medici', 'Ridolfi']
13 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Castellani', 'Barbadori', 'Medici', 'Tornabuoni', 'Ridolfi']
14 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Strozzi', 'Castellani', 'Barbadori', 'Medici', 'Ridolfi']
15 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Strozzi', 'Castellani', 'Barbadori', 'Medici', 'Tornabuoni', 'Ridolfi']
16 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Peruzzi', 'Strozzi', 'Ridolfi']
17 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Strozzi', 'Castellani', 'Barbadori', 'Medici', 'Ridolfi']
18 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Strozzi', 'Castellani', 'Barbadori', 'Medici', 'Tornabuoni', 'Ridolfi']
19 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Strozzi', 'Peruzzi', 'Castellani', 'Barbadori', 'Medici', 'Ridolfi']
20 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Strozzi', 'Peruzzi', 'Castellani', 'Barbadori', 'Medici', 'Tornabuoni', 'Ridolfi']
21 ['Lamberteschi', 'Guadagni', 'Bischeri', 'Strozzi', 'Ridolfi']

png

Level Up - Optional

  • Modify the plot_paths() to show each path in a different graph, with edge width showing the distance between source and target.

  • Prettify the graph entities further in order to make it more presentable

Summary

In this lab, we saw how to calculate and visualize paths between a given set of nodes in a graph. The skills learned in these simple exercises can be scaled to deal with much larger networks with possible tens of thousands of nodes (or maybe more) to identify paths between node entities. We mainly looked at calculating the simple distance, but the idea can be applied to directed and weighted networks with same level of ease.

dsc-3-28-07-graph-theory-simple-shortest-path-lab-online-ds-pt-100118's People

Contributors

shakeelraja avatar loredirick avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.