{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture17 - 1 We sday March 9 dne I ntro to Graphs rsals...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Wednesday, March 9 th Intro to Graphs Graph Traversals Dijkstra’s Algorithm Final Exam: Saturday, June 14, 2008, 3:00pm-6:00pm Final Exam Location: TBA
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Introduction to Graphs A graph is a finite set of dots called vertices (or nodes) connected by links called edges (or arcs). A given vertex can have multiple edges to other vertices. A given edge may be connected to at most two vertices. Question : When can an edge haveonly one vertex? Each vertex in the graph can hold any typeof data...
Image of page 2
3 Directed vs. Undirected Graphs In a directed graph , an edge goes from onevertex to another in a specific direction . For example, abovewe havean edgethat goes from 2 to 1, but not theother way around.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Directed vs. Undirected Graphs In an undirected graph , all edges arebi-directional. You can go either way along any edge. For example, wecan get from 2 to 4, and back from 4 to 2 along thesame edge.
Image of page 4
5 Graph Terminology A path : A path is a sequenceof adjacent vertices. U V W X Y Here’s a path from U to W.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Representing a Graph in Your Programs The easiest way to represent a graph is with a double-dimensional array . The size of each dimension of thearray is equal to the number of vertices in the graph. bool graph[5][5]; Each element in thearray indicates whether or not there is an edge between vertex i and vertex j.
Image of page 6
7 Each element in thearray indicates whether or not there is an edge between vertex i and vertex j. Representing a Graph in Your Programs bool graph[5][5]; 0 1 2 3 4 graph[0][3] = true; graph[1][2] = true; graph[3][0] = true; // edge from node 0 to node 3 This is called an adjacency matrix .
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Representing a Graph in Your Programs Exercise : What does thefollowing directed graph look like? Nodes 0 1 2 3 0 True False True False 1 True False False False 2 False False False True 3 True False True false Question : How do you represent the following undirected graph with an adjacency matrix? 0 1 2 3
Image of page 8
9 An Interesting Property of Adjacency Matrices Consider thefollowing graph: And it’s associated A.M.: Joe Mary Tsuen Lily Neato effect : If you multiply the matrix by itself something cool happens! 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 X = Joe Mary Tsuen Lily Mary Lily Joe Tsuen Joe Mary Tsuen Lily Mary Lily 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 Joe Tsuen The resulting matrix shows us which vertices are exactly two edges apart. 1 1
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Joe Mary Tsuen Lily Mary Lily 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 Joe Tsuen An Interesting Property of Adjacency Matrices Consider thefollowing graph: And it’s associated A.M.: Joe Mary Tsuen Lily Neato effect : If you multiply the matrix by itself something cool happens! 0 0 0 1 X = 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 And if we multiply our new matrix by the original matrix again, we’ll get all vertices that are exactly 3 edges apart ! 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 Joe Mary Tsuen Lily Mary Lily Joe Tsuen 0 1 1 0 0 1 0 0 0 1 1 So now you know how Friendster and Facebook work!
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern