0% found this document useful (0 votes)
97 views

Graph Theory and Path Searches in Python

This document discusses graph theory and algorithms for path searching in graphs using Python. It defines key graph theory concepts like vertices, edges, directed/undirected graphs, paths, and degrees. It also explains how to represent graphs as dictionaries in Python. The tasks involve implementing a graph, writing methods to output node degrees, find a path from node 6 to 1, and find all possible paths between those nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Graph Theory and Path Searches in Python

This document discusses graph theory and algorithms for path searching in graphs using Python. It defines key graph theory concepts like vertices, edges, directed/undirected graphs, paths, and degrees. It also explains how to represent graphs as dictionaries in Python. The tasks involve implementing a graph, writing methods to output node degrees, find a path from node 6 to 1, and find all possible paths between those nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB 4

Graph Theory and Path Searches in Python


Background

Graph Theory

Graph theory and in particular the graph ADT (abstract data-type) is widely explored and
implemented in the field of Computer Science and Mathematics. Consisting of vertices (nodes)
and the edges (optionally directed/weighted) that connect them, the data-structure is effectively
able to represent and solve many problem domains. One of the most popular areas of algorithm
design within this space is the problem of checking for the existence or (shortest) path between
two or more vertices in the graph. Properties such as edge weighting and direction are two such
factors that the algorithm designer can take into consideration. In this post I will be exploring
two of the simpler available algorithms, Depth-First and Breath-First search to achieve the goals
highlighted below:

 Find all vertices in a subject vertex connected component.

 Return all available paths between two vertices.

And in the case of BFS, return the shortest path (length measured by number of path edges).

In our illustration, - which is a pictorial representation of a graph, - the node "a" is connected
with the node "c", but "a" is not connected with "b". The connecting line between two nodes is
called an edge. If the edges between the nodes are undirected, the graph is called an undirected
graph. If an edge is directed from one vertex (node) to another, a graph is called a directed graph.
An directed edge is called an arc.

Python has no built-in data type or class for graphs, but it is easy to implement them in Python.
One data type is ideal for representing graphs in Python, i.e. dictionaries. 
The keys of the dictionary above are the nodes of our graph. The corresponding values are lists
with the nodes, which are connecting by an edge. There is no simpler and more elegant way to
represent a graph. 

An edge can be seen as a 2-tuple with nodes as elements, i.e. ("a","b")

Paths in Graph

We want to find now the shortest path from one node to another node.

 Adjacent vertices: 
Two vertices are adjacent when they are both incident to a common edge. 
 Path in an undirected Graph: 
A path in an undirected graph is a sequence of vertices P = ( v1, v2, ..., vn ) ∈ V x V x ...
x V such that vi is adjacent to v{i+1} for 1 ≤ i < n. Such a path P is called a path of length
n from v1 to vn. 
 Simple Path: 
A path with no repeated vertices is called a simple path. 

Degree of a Graph

The degree of a vertex v in a graph is the number of edges connecting it, with loops counted
twice. The degree of a vertex v is denoted deg(v). The maximum degree of a graph G, denoted
by Δ(G), and the minimum degree of a graph, denoted by δ(G), are the maximum and minimum
degree of its vertices. 

In the graph on the right side, the maximum degree is 5 at vertex c and the minimum degree is 0,
i.e the isolated vertex f. 
Lab Tasks

Implement the given Graph in Python.

1. Write a method to output degree of each node within the graph.

2. Write a method to find any path from node 6 to node 1 in given Graph.

3. Modify part 2 to show all possible paths between node 6 to node 1 in Graph.

DELIVERABLES:

1. Screenshots of all above enlisted tasks showing commands as well as results.


2. Task.py
3. Submit hard and soft copy of the report before deadline. Marks will be deducted for
late submissions.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy