Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Graphs.

Similar presentations


Presentation on theme: "Chapter 11 Graphs."— Presentation transcript:

1 Chapter 11 Graphs

2 Definitions A graph is two sets. Edges connect nodes.
A set of nodes or vertices V A set of edges E Edges connect nodes. The number of vertices |V| The number of edges |E| A sparse graph is one with relatively few edges A dense graph is one with relatively many edges A graph with all possible edges is complete

3 More Definitions A graph with edges directed from one vertex to another is a direct graph or a diagraph A graph whose edges are not directed is called an undirected graph A graph with labels associated with its vertices is called a labeled graph Two vertices that share an edge are called adjacent. They are also called neighbors. An edge connecting vertices u and v is written (u,v). The edge is said to be incident on u and v. Associated with each edge may be a cost or a weight.

4 Still More Definitions
A sequence of vertices v1, v2,…,vn forms a path of length n-1 if there exist edges from vi to vi+1 for 1 <= i < n. A path is simple if all vertices on the path are unique A cycle is a path of length 3 or more that connects some vertex to itself. A cycle is simple if the path is simple except for the first and last vertices.

5 Yes More Definitions A subgraph S is formed from Graph G by selecting a subset of Vs of G’s vertices and a subset Es of G’s edges such that for every edge E in Es, both whose vertices are in Vs. An undirected graph is connected if there is at least one path from any vertex to any other. A graph without cycles is called acyclic. A directed graph without cycles is called directed acyclic graph or DAG

6 Graph Representations
Adjacency Matrix If a pair of vertices have an edge between them, then there is a 1 in the matrix. Adjacency List If a vertex has an edge, then a node is added to its list with the other node as the data

7 Graph Implementations
A common activity that graphs must support is traversals This usually requires finding the node that is closest or the first node Then you usually need to find the next node after some given node. Another common member needed for a graph implementation is a way to mark each of the vertices

8 Graph Traversals To visit the vertices of a graph in some specific order based on the graph’s topology. There are some troublesome issues It may not be possible to reach all of the vertices from each other The graph may contain cycles and we need to make sure that the cycles do not cause an infinite loop

9 The Mark Graphs will typically maintain a mark for each vertex in the graph. They will clear the marks before a traversal begins and set the mark as they go. When the traversal is done we can check the marks to see if all the vertices have been visited.

10 Depth-first Search Whenever a vertex is visited, a DFS will recursively visit all of its unvisited neighbors. void DFS(Graph* G, int v) { Previsit(G, v); G->setMark(v, VISITED); for(int w=G->first(v); w<G->n(); w=G->next(v, w) ) if (G->getMark(v) == UNVISITED) DFS(G, w); PostVisit(G,v); }

11 Breadth-First Search Examines all vertices connected to the start vertex before visiting vertices further away Similar to DFS except a queue replaces the recursion stack. If the graph is a tree, this is equivalent to traversing level by level.

12 Shortest Paths Problem
Single-source shortest-path Given a vertex v in a graph G, what is the shortest path from v to all other vertices in G This is usually solved by a classic algorithm. Dijkstra’s Algorithm

13 Dijkstra’s Algorithm The algorithm maintains a distance estimate from each vertex to every other vertex. Initially, every distance is set to infinity. Vertices are process in order of distance. Whenever a vertex is processed, its distance is updated for all of its neighbors.

14 Minimum-Cost Spanning Trees
MST problem takes a connected, undirected, weighted graph. The MST is the graph containing the vertices of G along with the subset of G’s edges that Has minimum total cost as measured by summing the values for all of the edges in the subset Keeps the vertices connected.

15 Minimum-Cost Spanning Tree
There are two main algorithms for solving this problem Prim’s algorithm Kruskal’s algorithm


Download ppt "Chapter 11 Graphs."

Similar presentations


Ads by Google