Download presentation
Presentation is loading. Please wait.
Published byClinton Jordan Modified over 9 years ago
1
Graphs
2
Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes. The number of vertices |V| The number of vertices |V| The number of edges |E| The number of edges |E| A sparse graph is one with relatively few edges A sparse graph is one with relatively few edges A dense graph is one with relatively many edges A dense graph is one with relatively many edges A graph with all possible edges is complete 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 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 whose edges are not directed is called an undirected graph A graph with labels associated with its vertices is called a labeled 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. 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. 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. Associated with each edge may be a cost or a weight.
4
Still More Definitions A sequence of vertices v 1, v 2,…,v n forms a path of length n-1 if there exist edges from v i to v i+1 for 1 <= i < n. A sequence of vertices v 1, v 2,…,v n forms a path of length n-1 if there exist edges from v i to v i+1 for 1 <= i < n. A path is simple if all vertices on the path are unique 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 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. 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 V s of G’s vertices and a subset E s of G’s edges such that for every edge E in E s, both whose vertices are in V s. A subgraph S is formed from Graph G by selecting a subset of V s of G’s vertices and a subset E s of G’s edges such that for every edge E in E s, both whose vertices are in V s. An undirected graph is connected if there is at least one path from any vertex to any other. 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 graph without cycles is called acyclic. A directed graph without cycles is called directed acyclic graph or DAG A directed graph without cycles is called directed acyclic graph or DAG
6
Graph Representations Adjacency Matrix Adjacency Matrix –If a pair of vertices have an edge between them, then there is a 1 in the matrix. Adjacency List 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 A common activity that graphs must support is traversals This usually requires finding the node that is closest or the first node 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. 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 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. To visit the vertices of a graph in some specific order based on the graph’s topology. There are some troublesome issues 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. 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. 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. 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. 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 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 Examines all vertices connected to the start vertex before visiting vertices further away Similar to DFS except a queue replaces the recursion stack. Similar to DFS except a queue replaces the recursion stack. If the graph is a tree, this is equivalent to traversing level by level. If the graph is a tree, this is equivalent to traversing level by level.
12
Shortest Paths Problem Single-source shortest-path 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. This is usually solved by a classic algorithm. Dijkstra’s Algorithm Dijkstra’s Algorithm
13
Dijkstra’s Algorithm The algorithm maintains a distance estimate from each vertex to every other vertex. The algorithm maintains a distance estimate from each vertex to every other vertex. Initially, every distance is set to infinity. Initially, every distance is set to infinity. Vertices are process in order of distance. Vertices are process in order of distance. Whenever a vertex is processed, its distance is updated for all of its neighbors. 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. 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 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 There are two main algorithms for solving this problem –Prim’s algorithm –Kruskal’s algorithm
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.