Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Similar presentations


Presentation on theme: "Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs"— Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

2 Objects and Connections
Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another? Electrical circuits--Circuit elements are wired together. How does the current flow? Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. Links between web pages A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS. Graph Theory is a major branch of combinatorial mathematics.

3 Graphs A graph is a collection of vertices, V, and edges, E.
An edge connects two vertices. a d c b a d c b is the same as: Vertices: a, b, c, d Edges: ab, bc, ac, ad

4 Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. a d c b Example: A path from a to c could be ac or abc. A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. a d c b e Not Connected

5 More definitions A simple path is a path with no vertex repeated.
A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. Example: abca is a cycle a d c b A tree is a graph with no cycles. a d c b

6 Definitions Continued
A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges. a d c b Complete a d c b Dense a d c b Sparse

7 Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc. a d c b 34 22 13 19 4 Weighted a d c b Undirected a d c b Directed

8 Representing Graphs as an Adjacency List
An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 1 2 3 2 5 4 3 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

9 Representing Graphs as an Adjacency List
An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 2 5 1 2 1 5 4 3 3 2 2 4 3 5 4 2 5 3 4 1 2 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

10 Representing a Graph with an Adjacency Matrix
An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex. 1 2 3 5 4 The diagonal may be zero or one. Each edge is represented twice.

11 Representing Directed Graphs
In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 2 4 5 6 3 1 2 3 4 5 6 4 5 6

12 Representing Directed Graphs
In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 4 2 5 2 4 5 6 5 6 3 2 4 4 5 6 6

13 Representing Weighted Graphs
In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 1 3 6 5 7 2 4 4 5 6 1 3 1 2 3 4 5 6 4 5 6

14 Representing Weighted Graphs
In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 4 1 2 2 1 3 6 5 7 5 5 2 4 4 5 6 1 5 6 6 7 3 2 3 4 4 4 5 6 6 1

15 Lists vs. Matrices Speed:
An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

16 Breadth First Traversal
Problem: Given a graph, G = (V, E), find all the vertices reachable from some source, s. Repeat for all unvisited vertices Strategy: Visit all vertices adjacent to s first. Traversal expands outward level by level. (Visit all vertices a distance of 2 away from s second, then those at distance 3, etc.) Keep track of nodes that have been visited already (Otherwise may visit a vertex twice or end up in an endless cycle).

17 Pseudocode for Breadth First Traversal
template <int max_size> void Digraph <max_size>:: breadth_first(void (*visit)(vertex &)) const { Queue q; bool visited[max_size]; Vertex v, w, x; for (all v in G) visited[v] = false; for (all v in G) if (!visited[v]) { q.append(v); while(!q.empty( )) { q.retrieve(w); if(!visited[w]) { visited[w] = true; (*visit)(w); for (all x adjacent to w) q.append(x); } q.serve( );

18 Example a b c d e f g h We will work through this in class.


Download ppt "Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs"

Similar presentations


Ads by Google