One can manually count path lengths in a graph using adjacency matrices. Using the simple example below, produces the following adjacency matrix: A B A 1 1 B 1 0 This matrix means that given two vertices A and B in the graph above, there is a connection from A back to itself, and a two-way connection from A to B. To count the number of paths of length one, or direct connections in the graph, all one must do is count the number of 1s in the graph, three in this case, represented in letter notation as AA, AB, and BA. AA means that the connection starts and ends at A, AB means it starts at A and ends at B, and so on. However, counting the number of two-hop paths is a little more involved. The possibilities are AAA, ABA, and BAB, AAB, and BAA, making a total of five 2-hop paths. The 3-hop paths starting from A would be AAAA, AAAB, AABA, ABAA, and ABAB. Starting from B, the 3-hop paths are BAAA, BAAB, and BABA. Altogether, that would be eight 3-hop paths within this graph. Write a program taking any graph with at least two, but no more than five vertices, and count the number of paths of any length from 1 to 4 hops. Input from the keyboard two integers consisting of an integer N, indicating a graph with N nodes, followed by an integer P, the path length to calculate. Next, enter N strings, each of length N, containing 0s and 1s, indicating the adjacency matrix for the graph, separated by a space. Output the number of P length paths in the given matrix and the actual paths. The program must use a Graph data structure. Refer to the sample output below. Sample Run: How many nodes in the graph: 2 Path length to calculate: 1 Enter (0 or 1) the string for the adjacency matrix: 11 10 There are 3 1-length paths in the matrix: AA AB BA

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

One can manually count path lengths in a graph using adjacency matrices. Using the simple example
below, produces the following adjacency matrix:

A B
A 1 1
B 1 0



This matrix means that given two vertices A and B in the graph above, there is a connection from A back to
itself, and a two-way connection from A to B. To count the number of paths of length one, or direct
connections in the graph, all one must do is count the number of 1s in the graph, three in this case,
represented in letter notation as AA, AB, and BA. AA means that the connection starts and ends at A, AB
means it starts at A and ends at B, and so on. However, counting the number of two-hop paths is a little
more involved. The possibilities are AAA, ABA, and BAB, AAB, and BAA, making a total of five 2-hop paths.
The 3-hop paths starting from A would be AAAA, AAAB, AABA, ABAA, and ABAB. Starting from B, the 3-hop
paths are BAAA, BAAB, and BABA. Altogether, that would be eight 3-hop paths within this graph. Write a
program taking any graph with at least two, but no more than five vertices, and count the number of paths
of any length from 1 to 4 hops.
Input from the keyboard two integers consisting of an integer N, indicating a graph with N nodes,
followed by an integer P, the path length to calculate. Next, enter N strings, each of length N, containing 0s
and 1s, indicating the adjacency matrix for the graph, separated by a space. Output the number of P length
paths in the given matrix and the actual paths. The program must use a Graph data structure. Refer to the
sample output below.

Sample Run:

How many nodes in the graph: 2
Path length to calculate: 1
Enter (0 or 1) the string for the adjacency matrix: 11 10

There are 3 1-length paths in the matrix: AA AB BA

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Sets
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education