项目作者: Salma-Charad

项目描述 :
Determine whether the graph is only composed of cycles and paths. Then determine the number of vertices in each cycle or path found.
高级语言: Python
项目地址: git://github.com/Salma-Charad/Cycles-and-paths-in-graph.git
创建时间: 2020-11-09T15:46:35Z
项目社区:https://github.com/Salma-Charad/Cycles-and-paths-in-graph

开源协议:

下载


— Salma Charad —

Topic

Graph Theory

What is it about?

Given a graph, The code checks if it has only cycles and paths as components, and if so, The following details are given:

  • If the graph contains only cycles and paths or not
  • The number of vertices in each of the found cycles and paths.

How does it work

First, we check if every vertex is of degree 1 or 2. If any other degree is encountered, the graph is rejected.

Finding the paths :

  • We can start with a degree 1 vertex, and keep follwing next, next until we finish that path.
    We tore the number of vertices and separately keep track of whom you have visited.
  • Now we check if there is another unvisited degree 1 vertex. We start there and follow, follow until done.
  • This way we finish processing all the paths.

Finding the cycles:

  • Then, we process the cycles by starting at one place and following until we come back.
  • Along the way we should have recorded sizes of all the paths and cycle.

Sample Input 1

  1. enter number of vertices: 6
  2. enter number of edges: 4
  3. Enter edge: 1 2
  4. Enter edge: 2 3
  5. Enter edge: 4 5
  6. Enter edge: 5 6

Sample Output 1

  1. This graph has only paths and cycles as components.
  2. The graph contains 2 Path(s) of size 3

Sample Input 2

  1. enter number of vertices: 8
  2. enter number of edges: 7
  3. Enter edge: 1 2
  4. Enter edge: 2 3
  5. Enter edge: 3 4
  6. Enter edge: 4 1
  7. Enter edge: 5 6
  8. Enter edge: 6 7
  9. Enter edge: 7 8

Sample Output 2

  1. This graph has only paths and cycles as components.
  2. The graph contains 1 Path(s) of size 4
  3. The graph contains 1 Cycle(s) of size 4