Skip to content

YuriiBarninets/graph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python Graph Implementation

Created Graph, Vertex and Edge classes for representing graphs.
Added an ability to display graph image by using pydot, graphviz and PIL(Python Image Library).

Example of usage

Create a graph instance

from graph import Graph
graph = Graph() # Graph(directed=false) for undirected graph

Add vertices

graph.add_vertex("New York")
graph.add_vertex("Bratislava")
graph.add_vertex("Kyiv")
graph.add_vertex("Warsaw")
graph.add_vertex("Atlanta")

Add edges

graph.add_edge("New York", "Bratislava", 7)
graph.add_edge("Bratislava", "Warsaw", 3)
graph.add_edge("Warsaw", "New York", 12)
graph.add_edge("Warsaw", "Kyiv", 5)
graph.add_edge("Kyiv", "Bratislava", 4)
graph.add_edge("Atlanta", "Kyiv", 11)

Remove vertex

graph.remove_vertex("Atlanta")

Remove edge

graph.remove_edge("Atlanta", "Kyiv", 11)

Get all neighbours and edge's weight to them

# get Kyiv vertex and all outbound edges
vertex = graph.get_vertex("Kyiv")
outbound_edges = vertex.get_outbound_edges()

# iterate over outbound edges and get end vertex
for edge in outbound_edges:
  neighbour = edge.get_end_vertex()
  weight = edge.get_weight()

Display graph

from graph import display_graph
display_graph(graph, "Graph name")

Test graph

About

Graph implementation in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages