c455 05 graphs - computer science: indiana university ...danav/teach/c455/c455_10_graphs.pdf · 1...

23
1 ! ! " # " # $ % & & ( ( ) ) * * *+ # # *+ # # E A D B C A B C D F E undirected graph 5 vertices 7 edges directed graph 6 vertices 8 edges

Upload: others

Post on 19-Jan-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

1

����������� �������������������� ���������

����������������������

�� �� �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������� ����!���� ������������������������������� ����!��

�� "������������#����������"������������#����������$$ ����������%% ������������������ ����������&������������������������� �������&������������������������� �

�� ������������������������������������������������'���������(�������'���������(���������������������������)������������������������������)���������������������������������������������*��������������������������*�����������

�� ���������������������������������������������������������������������������������������������* +������������#�������������#�������������* +������������#�������������#������������

����������� �������������������� ���������

������������

E

A

D

B

C

A

B

C

D

F

E

undirected graph5 vertices7 edges

directed graph6 vertices8 edges

Page 2: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

2

����������� �������������������� ���������

������

�� ,���,���$$ ����������%% ������������ ����#������������������������� ����#������������������������������� ����'������������������� ����'�$$������%%(�������������(���������������������� ���������� �$$������%%!�������*�������������!�������*�������������

�� �������� ���������������-*�������������������������������-*����������������$$������%%����.���.����� �*�����������������+��#����$�������*�����������������+��#����$������//$��'�$��'�������0$0$(��� �(��� �������0$0$!�����������!�����������

�� ���������������������������������������������� ������������������������������������������������������������� ������������� ����$$ ����������

�� )���������������������������* +�����������������)���������������������������* +�����������������������,����������������1���������,����������������1���//$�$�

�� ������������������������������������������ ���������&����������������������&����������������������������������������� ������+��� ������������������������������������� ������+��� ������������������&����+��� ������ ������������������&����+��� ������ ���������

����������� �������������������� ���������

������������������������

�� ���*�������������������������*���������������������������������������������������������������������&���������������������������������������������&������������������������������������� ��&�������������������� ��&���������

�� 2�����������3��� "��4!�������*+����������������2�����������3��� "��4!�������*+�������������������� ����!���"����� ����!���"�⊆⊆ ������4�������4�⊆⊆ ������

�� �� ���������������������������������������������� �������������������������������*+��������������������������������#����������*+��������������������������������#���������������������������*�������&�������������������������������������*�������&�������������������������������������+���������������������������+���������

�� ���������������������������������������������� ������������������������������������+�������������+���������

Page 3: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

3

����������� �������������������� ���������

����������������������������������

�� )��������#������������������������������������)��������#����������������������������������������5������� ��&�����5������� ��&�

�� 4�������������������������������&�#��6������4�������������������������������&�#��6��������6�����������������������5�������������6�����������������������5�����������

�� 4�������5������� ��&��#��6������������������4�������5������� ��&��#��6������������������ ��&�#�����������#���������* ����-*���������� ��&�#�����������#���������* ����-*�����������* +������������������������* +�����������������������

�� "������������������'&���(�+����������"������������������'&���(�+������������57&87�8���$9����������������� ������������ ��&���57&87�8���$9����������������� ������������ ��&����:����:�

�� )�����5������� ��&���*����#���������* +�����)�����5������� ��&���*����#���������* +�������������+��'����������%(���������+��'����������%(�

����������� �������������������� ���������

��������� ������������� ����

�� ;����#��������;����#���������������� �������������� ������#���������* +��#���������* +��������������������������������

�� ��5������� ��&���5������� ��&������������������������������������������&� ����������&� �����

�� 4��*��*��������4��*��*����������������� ��&������������ ��&����� ������ ����

:: $$ :: $$ $$

$$ :: $$ $$ ::

:: $$ :: $$ $$

$$ $$ $$ :: ::

$$ :: $$ :: ::

A B C D E

A

B

C

D

E

Page 4: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

4

����������� �������������������� ���������

���������!������������!���

�� )��� ������ ���)��� ������ ��� ��� ����������������� ��� �����������������

�� �&� �������������������&� ����������������������������

�� ,���������������������,���������������������//������� �������������+��������� �������������+�������������������+��������������������+�����������������������

�� ,�������������#��������,�������������#������������#����������������������#����������������������������������������������������������5��������������5������������

A

B

C

D

E

F

3

0 4

3 5

1

2

0

����������� �������������������� ���������

����������������

class Graph {class Graph {struct vertex {struct vertex {list<int> neighbors;list<int> neighbors;string name;string name;

};};int nr_vertices, nr_edges;int nr_vertices, nr_edges;bool directed;bool directed;vector<vertex> vertices;vector<vertex> vertices;

};};

Page 5: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

5

����������� �������������������� ���������

�����"��������������������"���������������

void Graph::add_vertex(const void Graph::add_vertex(const string the_name)string the_name){{vertex new_vertex;vertex new_vertex;new_vertex.name = the_name;new_vertex.name = the_name;vertices.push_back(new_vertex)vertices.push_back(new_vertex)

;;nr_vertices++;nr_vertices++;

} }

����������� �������������������� ���������

void Graph::add_edge(const string name1, void Graph::add_edge(const string name1, const string name2)const string name2){{int index1 = index(name1);int index1 = index(name1);int index2 = index(name2);int index2 = index(name2);if (index1 >= nr_vertices)if (index1 >= nr_vertices)

add_vertex(name1);add_vertex(name1);if (index2 >= nr_vertices)if (index2 >= nr_vertices)

add_vertex(name2);add_vertex(name2);add_edge(index1, index2);add_edge(index1, index2);

}}void Graph::add_edge(const int index1, void Graph::add_edge(const int index1, const int index2)const int index2){{vertices[index1].neighbors.push_back(indevertices[index1].neighbors.push_back(inde

x2);x2);if (!directed)if (!directed)vertices[index2].neighbors.push_back(invertices[index2].neighbors.push_back(in

dex1);dex1);nr_edges++;nr_edges++;

} }

Page 6: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

6

����������� �������������������� ���������

#�������������$%��#�������������$%��&��$&��$

void make_graph1(Graph &gr)void make_graph1(Graph &gr){{for (char n='A'; n <= 'E'; for (char n='A'; n <= 'E';

n++)n++)add_char_vertex(gr, n);add_char_vertex(gr, n);

add_char_edge(gr, 'A', 'B');add_char_edge(gr, 'A', 'B');add_char_edge(gr, 'A', 'D');add_char_edge(gr, 'A', 'D');add_char_edge(gr, 'A', 'E');add_char_edge(gr, 'A', 'E');add_char_edge(gr, 'B', 'C');add_char_edge(gr, 'B', 'C');add_char_edge(gr, 'B', 'D');add_char_edge(gr, 'B', 'D');add_char_edge(gr, 'C', 'D');add_char_edge(gr, 'C', 'D');add_char_edge(gr, 'C', 'E');add_char_edge(gr, 'C', 'E');

} }

����������� �������������������� ���������

'������ '������

void Graph::make_empty()void Graph::make_empty(){{if (nr_vertices) {if (nr_vertices) {

for (int i=0;i<nr_vertices; i++)for (int i=0;i<nr_vertices; i++)vertices[i].neighbors.clear();vertices[i].neighbors.clear();

vertices.erase(vertices.begin(),vertices.erase(vertices.begin(),vertices.end());vertices.end());

nr_vertices = 0;nr_vertices = 0;nr_edges = 0;nr_edges = 0;

}}directed = false;directed = false;

}}

Page 7: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

7

����������� �������������������� ���������

void Graph::print() {void Graph::print() {int i;int i;cout <<"The graph contains " << nr_verticescout <<"The graph contains " << nr_vertices

<<" vertices and "<<nr_edges<<" edges" <<" vertices and "<<nr_edges<<" edges" << endl << "The vertex names are: ";<< endl << "The vertex names are: ";

for (i=0; i<nr_vertices; i++)for (i=0; i<nr_vertices; i++)cout << vertices[i].name << ' ';cout << vertices[i].name << ' ';

cout << endl << "The adjacency list for each" cout << endl << "The adjacency list for each" " vertex is:" << endl;" vertex is:" << endl;

for (i=0; i<nr_vertices; i++) {for (i=0; i<nr_vertices; i++) {cout << "vertex " << i << ": ";cout << "vertex " << i << ": ";list<int>::iterator iter;list<int>::iterator iter;iter = vertices[i].neighbors.begin();iter = vertices[i].neighbors.begin();while (iter!=vertices[i].neighbors.end()){while (iter!=vertices[i].neighbors.end()){

cout << *iter << ' ';cout << *iter << ' ';iter++;iter++;

}}cout << endl;cout << endl;

}}} }

����������� �������������������� ���������

(�����������������������(�����������������������

�� <�����#��������������������*��<�����#��������������������*������������������������������������������������������

�� )�����&������������������)�����&�������������������* +�������������* +������������

�� )����� ��������������������)����� ������������������������������#����������������������������#��������������������#����������+��#������#����������+��#����

�� )��������������������&��������)��������������������&����������������������#������� ����������������#������� ��������������'=(�+��#��������������������'=(�+��#����������������,���������������������,�������������#�����������������������������#�����������������������������#���������#���������

;;

�$%$�$%$

4��6�*�4��6�*�

)����)����

>�&������>�&������

?��?��

������

<�*���@�����?�����%%�<�*���@�����?�����%%�

���+��<�*���@��������+��<�*���@�����AB:CAB:C

@�����������*�����$D�$:@�����������*�����$D�$:

<����������*�����B::A<����������*�����B::A

Page 8: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

8

����������� �������������������� ���������

void Graph::read(const char *filename)void Graph::read(const char *filename){{ifstream fin(filename);ifstream fin(filename);char buffer[20], buffer1[20];char buffer[20], buffer1[20];int nrv;int nrv;fin >> buffer; // U or Dfin >> buffer; // U or Dif (buffer[0] == 'd' || buffer[0] == 'D')if (buffer[0] == 'd' || buffer[0] == 'D')

directed=true;directed=true;fin >> nrv;fin >> nrv;for (int i=0; i<nrv; i++) {for (int i=0; i<nrv; i++) {

fin >> buffer; // vertex namefin >> buffer; // vertex nameadd_vertex(string(buffer));add_vertex(string(buffer));

}}while (!fin.eof()) {while (!fin.eof()) {

fin >> buffer;fin >> buffer;buffer[strlen(buffer)buffer[strlen(buffer)--1]='1]='\\0';0';fin >> buffer1;fin >> buffer1;if (strlen(buffer) && strlen(buffer1))if (strlen(buffer) && strlen(buffer1))add_edge(string(buffer), add_edge(string(buffer),

string(buffer1));string(buffer1));}}fin.close();fin.close();

} }

����������� �������������������� ���������

����)����������)������

�� )��������#����������������������)��������#���������������������������� ���+����������� ���+�����//��������������������������//��������

�� )���+�����)���+�����//������������ �������&������������������� �������&��������&��������������������+��������������������&��������������������+�����������������������+��������������+������������������+��������������+��������������

�� )��������������������� �������&�����)��������������������� �������&������&����������������������������������������&������������������������������������������#���*������������������&��"���������#���*������������������&��"���������1����������*��������� ���+��6������������1����������*��������� ���+��6���������������������������������

Page 9: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

9

����������� �������������������� ���������

#�����#�����**����������

�� ,����+����������������#������������ ������&�����,����+����������������#������������ ������&������������������� �6������������+����E������E����������������� �6������������+����E������E���

�� )����#����6�����������������+������*����������)����#����6�����������������+������*���������������������� ����������������+��������������������������� ����������������+�������������������+������ ������������ �6�������������+�������+������ ������������ �6�������������+������������+����������������������+�������������

�� )�������������������������+������6������������)�������������������������+������6������������� ���#��������������������*�������� ������� ���#��������������������*�������� ����������������� �6���������+����������������������������� �6���������+����������������������������������������������

�� "�������*��������#���*������������������������"�������*��������#���*������������������������+��������������+���� �6����+��������������+���� �6����

����������� �������������������� ���������

breadth_first_search (graph G, int v) {breadth_first_search (graph G, int v) {initialize all vertices as "unmarked";initialize all vertices as "unmarked";queue<int> Q;queue<int> Q;process and mark v;process and mark v;Q.enqueue(v);Q.enqueue(v);while (Q is not empty) {while (Q is not empty) {

x = Q.dequeue();x = Q.dequeue();for each neighbor y of x in G {for each neighbor y of x in G {process the edge from x to y if process the edge from x to y if

necessary;necessary;if (y has not been marked) {if (y has not been marked) {

process and mark y;process and mark y;enqueue (y, Q);enqueue (y, Q);

}}}}perform final processing on vertex x;perform final processing on vertex x;

}}}}

Page 10: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

10

����������� �������������������� ���������

���������+���������+**,-,-

����������� �������������������� ���������

���������+���������+**,.,.

Page 11: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

11

����������� �������������������� ���������

��� ���**������������ "������������ ������&�������������������� �6���"������������ ������&�������������������� �6������������+����������������������+�������������

�� )����#�����������������������+������������#���)����#�����������������������+������������#��������������������� ���������#�������� �6��#����������������������� ���������#�������� �6��#�����������+�������������������+�������������

�� )����#������������*� �6�������+������#�����������)����#������������*� �6�������+������#�����������&��������������������� ��#������&������� �6��&��&��������������������� ��#������&������� �6��&��������������������������

�� "��6������������E���������������E���������"��6������������E���������������E��������������*����#������������������ ���*� �6��������*����#������������������ ���*� �6�������������������

�� )����#��+��6���6�*����#������+��6����������������)����#��+��6���6�*����#������+��6�����������������������*� �6�������+�������#���������#��������������*� �6�������+�������#���������#�������������������

�� "�������*�������������*����#���&��*�������"�������*�������������*����#���&��*�����������+����������� ���������������&��������+����������� ���������������&����

����������� �������������������� ���������

Graph::depth_first_search (int v) { Graph::depth_first_search (int v) { bool mark[nr_vertices];bool mark[nr_vertices];for (int i=0; i< nr_vertices; i++)for (int i=0; i< nr_vertices; i++)

mark[i]=false; mark[i]=false; stack<(int, vertex*)> S;stack<(int, vertex*)> S;mark[v]=true; // perform preliminary processing on v;mark[v]=true; // perform preliminary processing on v;p = vertices[v].neighbors.begin();p = vertices[v].neighbors.begin();S.push(v,p);S.push(v,p);while (S is not empty) {while (S is not empty) {

(v,p)=S.pop();//perform intermediate processing on v;(v,p)=S.pop();//perform intermediate processing on v;while (p != vertices[v].neighbors.end()) {while (p != vertices[v].neighbors.end()) {

w = *p; // process the edge from v to w;w = *p; // process the edge from v to w;p++; p++; if (!mark[w]) { // w has not been reached)if (!mark[w]) { // w has not been reached)

S.push(v,p);S.push(v,p);v = w;v = w;mark[v] = true; //perform preliminary processing mark[v] = true; //perform preliminary processing

on v;on v;p = vertices[v].neighbors.begin(); p = vertices[v].neighbors.begin();

}}}}perform final processing on vertex v;perform final processing on vertex v;

}}}}

Page 12: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

12

bool mark[nr_vertices]; bool mark[nr_vertices]; Graph::depth_first_search(int v) {Graph::depth_first_search(int v) {for (int i=0; i< nr_vertices; i++)for (int i=0; i< nr_vertices; i++)

mark[i]=false;mark[i]=false;recursive_depth_first_search(v);recursive_depth_first_search(v);

} }

Graph::recursive_depth_first_search (int v) { Graph::recursive_depth_first_search (int v) { mark[v] = true;mark[v] = true;perform preliminary processing on v;perform preliminary processing on v;p = vertices[v].neighbors.begin();p = vertices[v].neighbors.begin();while (p != vertices[v].neighbors.end()) {while (p != vertices[v].neighbors.end()) {

perform intermediate processing on v if necessary;perform intermediate processing on v if necessary;w = *p;w = *p;process the edge from v to w if necessary;process the edge from v to w if necessary;p++;p++;if (!mark[w])if (!mark[w])

recursive_depth_first_search (w); recursive_depth_first_search (w); }}perform final processing of vertex v;perform final processing of vertex v;

}}

����������� �������������������� ���������

(���������������%����(���������������%����

�� 3�#�����#��������������������� ����������������3�#�����#��������������������� ��������������������������#���������#��������+��������F�����������#���������#��������+��������F�

�� ,��������+�������#�������+�����,��������+�������#�������+�����//���������� ����������� ����������������������������������&����������������������������������&�

�� 4�������#����#�� �6�����#�����&��#��������*���4�������#����#�� �6�����#�����&��#��������*��������* +�����������������������������* +������������������������

�� ������ ��#�������#����������#������������������� ��#�������#����������#���������������������������������������������+��$���������������������������������+��$�

�� )�������� ��#������*��������������#���+���������)�������� ��#������*��������������#���+�����������������������������������

Page 13: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

13

����������� �������������������� ���������

!��������������!��������������

�� 4����#�������������#��#�*����6���������4����#�������������#��#�*����6�������������������*�������������* ��������#����������������*�������������* ��������#������������������������� � ���������������������� � ���

�� )���+����6��#�������� �#�������������)���+����6��#�������� �#�������������$G�G�+�����256����$G�G�+�����256����

�� ,�1���� ����������������@4������� �*����,�1���� ����������������@4������� �*�����������-*�*����������������� �����������-*�*����������������� ����-*�*��-*�*��

�� )���-*�*��#��������������������������������)���-*�*��#��������������������������������������������&����6��#��*�����������������������������&����6��#��*��������������������������� ����������� �

����������� �������������������� ���������

��'���/����������� ��'���/�����������

�� )��������� �������#��������������������������)��������� �������#��������������������������������������������������

�� "��#����������������������������&������������"��#����������������������������&������������������������������� ��������������������������������� ��������������

�� "���������������6��#�������������&�����"���������������6��#�������������&���������������������������� ������������������������������������� ��������������

�� "��#����������������������������������������H���"��#����������������������������������������H����������4����������H���������������+����������� ��������4����������H���������������+����������� ��������������6��#����������������������������������������6��#���������������������������������������������������

�� )��������� �������#����������������������+����)��������� �������#����������������������+��������H�������H���

Page 14: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

14

����������� �������������������� ���������

��'�����������0���� ��'�����������0����

Initialization()Initialization(){{for (int i=0; i<nr_vertices; i++) {for (int i=0; i<nr_vertices; i++) {

vertices[i].total_cost = +infinity;vertices[i].total_cost = +infinity;vertices[i].finalized = false;vertices[i].finalized = false;

}}Priority_min_queue PQ; Priority_min_queue PQ; vertices[origin].total_cost = 0;vertices[origin].total_cost = 0;vertices[origin].predecessor = vertices[origin].predecessor = --1; 1; PQ.push(origin, 0);PQ.push(origin, 0);

}}

����������� �������������������� ���������

dijkstra_algorithm (graph G, int origin, int dijkstra_algorithm (graph G, int origin, int destination) {destination) {Initialization(); // should return PQ somehow.Initialization(); // should return PQ somehow.while (PQ is not empty) {while (PQ is not empty) {

x = PQ.pop();x = PQ.pop();if (vertices[x].finalized) continue;if (vertices[x].finalized) continue;vertices[x].finalized = TRUE;vertices[x].finalized = TRUE;if (x == destination)if (x == destination)return;return;

for each nonfor each non--finalized neighbor y of xfinalized neighbor y of xif (vertices[x].total_cost + cost(x,y) <if (vertices[x].total_cost + cost(x,y) <

vertices[y].total_cost) {vertices[y].total_cost) {vertices[y].total_cost=vertices[y].total_cost=

vertices[x].total_cost + cost(x,y);vertices[x].total_cost + cost(x,y);vertices[y].predecessor = x;vertices[y].predecessor = x;PQ.push(y, vertices[y].total_cost);PQ.push(y, vertices[y].total_cost);

}}}}

}}

Page 15: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

15

����������� �������������������� ���������

������������������total_cost, finalized, predecessor, edge list

����������� �������������������� ���������

�������(������)��� �������(������)���

�� �� �������������������*��������������������������*�������������������������������������������������������

�� �� ����������������������������������������������������������������������������������������������� ������&������������������������������� ������&��������������������������

�� ��������*���������#��������������������*���������#��������������'���(��������������������)��*����������'���(��������������������)��*�������������* ��������#�����������������������������* ��������#������������������������ � ��� � ���

Page 16: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

16

����������� �������������������� ���������

����������

����������� �������������������� ���������

1���'�/������������1���'�/������������2,34567�2,34567�

�� I�����'����(�I�����'����(��� ,����H��)���'����)(����)���,����H��)���'����)(����)���∅∅ // ����������������������������������������

�� I�������� ������������ ������������)!�I�������� ������������ ������������)!��� #����'J��)�J��K���#����'J��)�J��K���// $�������������$��������������&��*����(� �&��*����(� LL M� ��������#������������������ ���+!��� ���9M� ��������#������������������ ���+!��� ���9LL ,��������+�+���������������������������,��������+�+����������������������������� ������������)�������������������)�������� ��� ������������)�������������������)�������� �����*������������#���� �����������������������*������������#���� �����������������������+������������������� �����������������+������������������� �������������

Page 17: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

17

����������� �������������������� ���������

)���������(������)���������(������

�� <*������#�������������� ���<*������#�������������� ����* +��������6����� �����#�����* +��������6����� �����#�������������������������#���������������������������#������������������������ �������������������� ��

�� "����������H���������6������"����������H���������6��������������������������������������

�� "�� *�����������������������"�� *��������������������������6������������������6�����������������������������������������

�� �&� �����)$��)C��)D��)���)���)%��&� �����)$��)C��)D��)���)���)%�

)$

)D

)�

)�

)C

)%

����������� �������������������� ���������

)������%���)������%���

�� )���)���������(����������%���)���)���������(����������%�������������������������������������������+�����������������������������+���������//������������������������������������������������*����������������������������������������������*������������������������������������&�������������������'&���(�������������������������&�������������������'&���(�����������������������&�������������������������������&�����������������������

�� ������������������������#�������������������������������������������#����������������������������������������������������������������������������������������������������������������������*���������������������������*������������������������������������������������������������������������������������������������������������

�� ,��#���* +�������������������������#����,��#���* +�������������������������#������������������*����������#�����������#�����������������������*����������#�����������#����������������������%������������������%�����������������������������������

�� 8���8�����,��������������������������������������������,������������������������������������������+���������������������+���������������������

Page 18: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

18

����������� �������������������� ���������

boolean topologically_number (digraph G)boolean topologically_number (digraph G){{for (ever vertex v in G) {for (ever vertex v in G) {

visited[v] = false;visited[v] = false;numbered[v] = false;numbered[v] = false;

}}int topol_counter = number_of_vertices(G);int topol_counter = number_of_vertices(G);for (each vertex v in G)for (each vertex v in G)

if (!visited[v]) {if (!visited[v]) {visited[v] = true;visited[v] = true;if (!recursively_number(G, v, if (!recursively_number(G, v,

topol_counter))topol_counter))return false;return false;

}}return true;return true;

}}

����������� �������������������� ���������

boolean recursively_number (digraph G, int v, boolean recursively_number (digraph G, int v, int & counter)int & counter)

{{for (each w that can be reached from v) {for (each w that can be reached from v) {

if (numbered[w]);if (numbered[w]);else if (visited[w]) // but not numberedelse if (visited[w]) // but not numberedreturn false; // a cycle has been foundreturn false; // a cycle has been found

else {else {visited[w] = true;visited[w] = true;if (!recursively_number (G, w, counter))if (!recursively_number (G, w, counter))

return false; return false; }}

} } vertex[v].topol_number = counter; vertex[v].topol_number = counter; ---- counter;counter;return true; // no cycles were detectedreturn true; // no cycles were detected

}}

Page 19: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

19

����������� �������������������� ���������

����������

����������� �������������������� ���������

)����������������)����������������

�� &��������&�����������������*�����������������������*����������������*������������������������������&���*������������������������������&����������������������?���������������������?//�� �������� ������

�� �������������� ���������*����������������*�����������*����������������*��������������������������������������������������������������������������������

�� )��������(��������%���)��������(��������%�����,��������������,�����������������������#��������������������������������#���������������������3� ���������*�� � H�������������������3� ���������*�� � H��������������������?�?//�� �������� ������

Page 20: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

20

����������� �������������������� ���������

������������������������������

�� I�����������������*�������������������I�����������������*���������������������������������

�� �������������������������� ���������������������������������������*�������������������&�������������������*�������������������&���������������������������������������������� ������*���������������������*����������������������������� ������&����#���������������������������� ������&����#��������������

�� ��9 ��9)���)��������������� ���������&�����������������&��������*��������������������* +�����������*��������������������* +������������������+�����������������+����������

����������� �������������������� ���������

)�������)��������� )������)�������� I���?�������������������*����������������I���?�������������������*���������������������������������������&����?��<*�����������?�������������������������������&����?��<*�����������?���������&��������������������������������*����������&��������������������������������*����������(�(,�����������������������������?� *���������������,�����������������������������?� *��������������������������+*������������+���������������+*������������+����

+(+(,������������������������������?� *���+�������������,������������������������������?� *���+�������������������������������� *���������������������������������������������� *���������������������������

�� �������������������� ,����������������� ���������#���������#�������,����������������� ���������#���������#���������������������������������������*�������������������������������������������*�����������

�� ,������������������&�������#���������#��������������,������������������&�������#���������#������������������������*���������� *�����������������������������������������*���������� *���������������������������������������������������'������������+������*�������*�(����������������������'������������+������*�������*�(��

�� ,������������������������#�����������������������,������������������������#������������������������*�������������� *���+������*�������*���*�������������� *���+������*�������*��

Page 21: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

21

����������� �������������������� ���������

�����/����������������/�����������

Print_Eulerian (graph G, int v)Print_Eulerian (graph G, int v){{for each nonfor each non--eliminated edge {v,w} {eliminated edge {v,w} {

mark the edge {v,w} as eliminated;mark the edge {v,w} as eliminated;Print_Eulerian (G, w);Print_Eulerian (G, w);

}}print v;print v;

}}

����������� �������������������� ���������

����������

Page 22: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

22

����������� �������������������� ���������

���������� ����������

�� I�������+����#�������������#�����������&�����I�������+����#�������������#�����������&�����''������������(�������������� ��������������������(�������������� ������������������������&�����'����&�����'���'���'(������������*�������������)���(������������*�������������)���#�������������������#���+���������#�������������������#���+�������������������������

�� 4����������� �������4����������� ����������������� '���'���//�����������������* +��(�������������������*�����������������#����* +��(�������������������*�����������������#�������������������������������������������$($(��������#������������������������������-*������������������#������������������������������-*������������������������������9��������������������9

%(%(����������� ����������&�����������* �����������#���������������� ����������&�����������* �����������#����������������������� ������-*�����������* �����������#�����������������-*�����������* �����������#������������*��*� �������'E�����������������#E(9���'E�����������������#E(9

D(D(�����* �����������#���������������������������+��������* �����������#���������������������������+���

����������� �������������������� ���������

����������

Page 23: C455 05 graphs - Computer Science: Indiana University ...danav/teach/c455/C455_10_graphs.pdf · 1 ˘ ˇˆˇ ˙ ˝ ˘ ˆˇ ˙ ˙ ˛ ˚ ˜ ˆˇ˝! " #˘ ˘$ ˙˘% & ˙ ˙ ’ ˙ ( ˙

23

����������� �������������������� ���������

������������**1�����������1�����������

�� <����#�������������#������'#������������<����#�������������#������'#���������������#�����:(����#�����:(�

�� M���������������#���������#��������+���M���������������#���������#��������+���

LL 4������������ �������*������������6�������4������������ �������*������������6����������*���������*��������������������#������������*���������*��������������������#���������������������:�������������:�

LL �������� � ���#������������������������������������� � ���#��������������������������������������������#����������� �������� ��������������������#����������� �������� ��������*�����������*��������

����������� �������������������� ���������

����������