0

I was recently given an assignment that requires the need to construct an undirected unweighted graph in order to later on use it in a BFS algorithm. For some background, the adjacency matrix stores vertexes in string form, and its edges in string form. As an example, there would be two movie actor vertexes that are connected through a movie they worked together. The relationship between the vertexes and movies will be later used to find the shortest path between two actors. I am still rather a novice when it comes to programming and c++, however I would appreciate any help as to how I would get started and progress through it.

Just as a preface, I am NOT asking for actual code. That would be against integrity guidelines at my school, and moreover, I am trying to learn the material rather than regurgitating algorithms into my IDE.

My main question is how do I create and store the strings representing the actors in the matrix and the strings representing the edges between the actors in the individual cells of the matrix.

  • Welcome to stackoverflow.com! Please clarify your question with what would be a good answer to your question. – Hack06 Jun 03 '20 at 11:09
  • Just like a walkthrough as to what would be required in order to implement the matrix, and thanks! – Freshzak187 Jun 03 '20 at 11:13
  • 2
    It's very hard to write a walkthrough without knowing what you need help with. It'd become a textbook in both graph theory and programming. – Ted Lyngmo Jun 03 '20 at 11:14
  • A fast googling result: http://embedonix.com/articles/image-processing/matrix-implementation-and-operations-in-c-part-1/ – Hack06 Jun 03 '20 at 11:15
  • You could (for example) start by implementing a generic M\*N matrix class. Then you could use this class together with a vector of strings (your vertices) to implement the whole adjacency matrix. *But* - and that's probably the point of the other comments - we don't know whether your level of skill allows to implement a generic matrix class on your own. – grek40 Jun 03 '20 at 11:15
  • I updated the qeustion @TedLyngmo – Freshzak187 Jun 03 '20 at 11:17
  • Store strings as `std::string`. Anything else? – grek40 Jun 03 '20 at 11:18
  • So instead of using an int datatype for conventional matrices, I could get away with using std::string? Moreover, how could I tell that the two vertexes have an edge in common? – Freshzak187 Jun 03 '20 at 11:21
  • @Freshzak187 -- Let's start with this -- If it were not strings, how would you write the program? What datatype would you use? Your question seems to ask how to represent the data, so start with the type you would know how to handle. – PaulMcKenzie Jun 03 '20 at 12:08
  • @PaulMcKenzie, I've began to implement an adjacency matrix where I have the edges contained in a vector>, and I'm using an iterator to find the from and to nodes. However, traditionally when there is a relationship between 2 vertexes, I would set the vector[][] == 1, but how can I retain the information of the edge, ie the movie that is connecting both the actor vertexes? – Freshzak187 Jun 03 '20 at 13:13
  • What if it were integers and not strings? What would your definition of the adjacency matrix be? That was my question to you, but you focused on strings. What if the type were double, or float, or Widgets, and not strings? You could always have a vector of `std::pair` to describe the node and edges, where `SomeType` is whatever data the node represents, and the `int` is the universal way of representing the adjacency matrix. – PaulMcKenzie Jun 03 '20 at 13:18
  • hmmm, would it just be a 2d array of ints where one represents the rows and the other columns? – Freshzak187 Jun 03 '20 at 13:23
  • More likely a 2D array of `std::pair` where `x` is the data type that the node is referring to. Yes, the code would need to use `pair.first` to get to the `int`, but you also get the data as part of the `pair` packaged with it by using `pair.second`. Then you could even write a template class based on `x`, and the code will work for any type of `x`. – PaulMcKenzie Jun 03 '20 at 13:25
  • Ah, so in order to retain the information that comes with the edge, instead of using a nested vector, it would be better to use a vector of pairs such that ? – Freshzak187 Jun 03 '20 at 13:27
  • `struct MovieInfo { std::string actor; std::string movie; };`, then `std::pair`. and a 2D vector of those pairs. Basically you are still writing the 2D in a conventional fashion, just that the edges and vertices have the "traditional" `int` type, with an added payload of the data the edge/vertex represents. – PaulMcKenzie Jun 03 '20 at 13:29
  • Just for more clarification, when setting the matrix[][] == 1 with the usage of a pair, the edge becomes established, and the information of what movie is stored along with it? – Freshzak187 Jun 03 '20 at 13:34
  • Basically `matrix[x][y] = {1, {"John Wayne", "Western"}};` etc. – PaulMcKenzie Jun 03 '20 at 13:36
  • Wow, thank you very much – Freshzak187 Jun 03 '20 at 13:40

0 Answers0