This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Wednesday, March 11 th Intro to Graphs Graph Traversals Dijkstras Algorithm Final Exam: Saturday, June 14, 2008, 3:00pm6:00pm Final Exam Location: TBA 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 have only one vertex? Each vertex in the graph can hold any type of data... Directed vs. Undirected Graphs In a directed graph , an edge goes from one vertex to another in a specific direction . For example, above we have an edge that goes from 2 to 1, but not the other way around. Directed vs. Undirected Graphs In an undirected graph , all edges are bidirectional. You can go either way along any edge. For example, we can get from 2 to 4, and back from 4 to 2 along the same edge. Graph Terminology A path : A path is a sequence of adjacent vertices. U V W X Y Heres a path from U to W. Representing a Graph in Your Programs The easiest way to represent a graph is with a doubledimensional array . The size of each dimension of the array is equal to the number of vertices in the graph. bool graph[5][5]; Each element in the array indicates whether or not there is an edge between vertex i and vertex j. Each element in the array indicates whether or not there is an edge between vertex i and vertex j. Representing a Graph in Your Programs bool graph[5][5]; 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 . Representing a Graph in Your Programs Exercise : What does the following directed graph look like? Nodes 1 2 3 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? 1 2 3 An Interesting Property of Adjacency Matrices Consider the following graph: And its associated A.M.: Joe Mary Tsuen Lily Neato effect : If you multiply the matrix by itself something cool happens! 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 X = Joe Mary Tsuen Lily M a r y L i l y 0 1 0 0 0 1 0 0 0 1 0 1 0 J o e T s u e n Joe Mary Tsuen Lily M a r y L i l y 0 0 1 0 0 0 1 0 1 0 0 0 1 J o e T s u e n The resulting matrix shows us which vertices are exactly two edges apart. 1 1 Joe Mary Tsuen Lily M a r y L i l y 0 1 0 0 0 1 0 0 0 1 0 1 0 J o e T s u e n An Interesting Property of Adjacency Matrices Consider the following graph: And its associated A.M.: Joe Mary Tsuen Lily Neato effect : If you multiply the matrix by itself something cool happens!...
View Full
Document
 Spring '09
 John

Click to edit the document details